PageRenderTime 45ms CodeModel.GetById 16ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/README.md

https://github.com/chendong0444/gocrawl
Markdown | 258 lines | 170 code | 88 blank | 0 comment | 0 complexity | 63d2466485712925b419eff75882b689 MD5 | raw file
  1# gocrawl
  2
  3gocrawl is a polite, slim and concurrent web crawler written in Go.
  4
  5For a simpler yet more flexible web crawler you may also want to take a look at [fetchbot](https://github.com/PuerkitoBio/fetchbot), a package that builds on the experience of gocrawl.
  6
  7## Features
  8
  9*    Full control over the URLs to visit, inspect and query (using a pre-initialized [goquery][] document)
 10*    Crawl delays applied per host
 11*    Obedience to robots.txt rules (using the [robotstxt.go][robots] library)
 12*    Concurrent execution using goroutines
 13*    Configurable logging
 14*    Open, customizable design providing hooks into the execution logic
 15
 16## Installation and dependencies
 17
 18gocrawl depends on the following userland libraries:
 19
 20*    [goquery][]
 21*    [purell][]
 22*    [robotstxt.go][robots]
 23
 24To install:
 25
 26`go get github.com/PuerkitoBio/gocrawl`
 27
 28To install a previous version, you have to `git clone https://github.com/PuerkitoBio/gocrawl` into your `$GOPATH/src/github.com/PuerkitoBio/gocrawl/` directory, and then run (for example) `git checkout v0.3.2` to checkout a specific version, and `go install` to build and install the Go package.
 29
 30## Changelog
 31
 32*    **v0.4.1** : now go-getable, since goquery is go-getable too.
 33*    **v0.4.0** : **BREAKING CHANGES** major refactor, API changes:
 34    * Use an `*URLContext` structure as first argument to all `Extender` interface functions that are called in the context of an URL, instead of a simple `*url.URL` pointer that was sometimes normalized, sometimes not.
 35    * Remove the `EnqueueOrigin` enumeration flag. It wasn't even used by gocrawl, and it is a kind of state associated with the URL, so this feature is now generalized with the next bullet...
 36    * Add a `State` for each URL, so that the crawling process can associate arbitrary data with a given URL (for example, the ID of a record in the database). Fixes [issue #14][i14].
 37    * More idiomatic use of errors (`ErrXxx` variables, `Run()` now returns an error, removing the need for the `EndReason` enum).
 38    * Much simplified `Filter()` function. It now only returns a `bool` indicating if it should be visited or not. The HEAD request override feature is provided by the `*URLContext` structure, and can be set anywhere. The priority feature was unimplemented and has been removed from the return values, if it gets implemented it will probably be via the `*URLContext` structure too.
 39    * `Start`, `Run`, `Visit`, `Visited` and the `EnqueueChan` all work with the empty interface type for the URL data. While this is an inconvenience for compile-time checks, it allows for more flexibility regarding the state feature. Instead of always forcing a `map[string]interface{}` type even when no state is needed, gocrawl supports [various types](#types).
 40    * Some other internal changes, better tests.
 41*    **v0.3,2** : Fix the high CPU usage when waiting for a crawl delay.
 42*    **v0.3.1** : Export the `HttpClient` variable used by the default `Fetch()` implementation (see [issue #9][i9]).
 43*    **v0.3.0** : **BEHAVIOR CHANGE** filter done with normalized URL, fetch done with original, non-normalized URL (see [issue #10][i10]).
 44*    **v0.2.0** : **BREAKING CHANGES** rework extension/hooks.
 45*    **v0.1.0** : Initial release.
 46
 47## Example
 48
 49From `example_test.go`:
 50
 51```Go
 52package gocrawl
 53
 54import (
 55  "github.com/PuerkitoBio/goquery"
 56  "net/http"
 57  "regexp"
 58  "time"
 59)
 60
 61// Only enqueue the root and paths beginning with an "a"
 62var rxOk = regexp.MustCompile(`http://duckduckgo\.com(/a.*)?$`)
 63
 64// Create the Extender implementation, based on the gocrawl-provided DefaultExtender,
 65// because we don't want/need to override all methods.
 66type ExampleExtender struct {
 67  DefaultExtender // Will use the default implementation of all but Visit() and Filter()
 68}
 69
 70// Override Visit for our need.
 71func (this *ExampleExtender) Visit(ctx *URLContext, res *http.Response, doc *goquery.Document) (interface{}, bool) {
 72  // Use the goquery document or res.Body to manipulate the data
 73  // ...
 74
 75  // Return nil and true - let gocrawl find the links
 76  return nil, true
 77}
 78
 79// Override Filter for our need.
 80func (this *ExampleExtender) Filter(ctx *URLContext, isVisited bool) bool {
 81  return !isVisited && rxOk.MatchString(ctx.NormalizedURL().String())
 82}
 83
 84func ExampleCrawl() {
 85  // Set custom options
 86  opts := NewOptions(new(ExampleExtender))
 87  opts.CrawlDelay = 1 * time.Second
 88  opts.LogFlags = LogAll
 89
 90  // Play nice with ddgo when running the test!
 91  opts.MaxVisits = 2
 92
 93  // Create crawler and start at root of duckduckgo
 94  c := NewCrawlerWithOptions(opts)
 95  c.Run("https://duckduckgo.com/")
 96
 97  // Remove "x" before Output: to activate the example (will run on go test)
 98
 99  // xOutput: voluntarily fail to see log output
100}
101```
102
103## API
104
105Gocrawl can be described as a minimalist web crawler (hence the "slim" tag, at ~1000 sloc), providing the basic engine upon which to build a full-fledged indexing machine with caching, persistence and staleness detection logic, or to use as is for quick and easy crawling. Gocrawl itself does not attempt to detect staleness of a page, nor does it implement a caching mechanism. If an URL is enqueued to be processed, it *will* make a request to fetch it (provided it is allowed by robots.txt - hence the "polite" tag). And there is no prioritization among the URLs to process, it assumes that all enqueued URLs must be visited at some point, and that the order in which they are is unimportant.
106
107However, it does provide plenty of [hooks and customizations](#hc). Instead of trying to do everything and impose a way to do it, it offers ways to manipulate and adapt it to anyone's needs.
108
109As usual, the complete godoc reference can be found [here][godoc].
110
111### Design rationale
112
113The major use-case behind gocrawl is to crawl some web pages while respecting the constraints of `robots.txt` policies and while applying a *good web citizen* crawl delay between each request to a given host. Hence the following design decisions:
114
115* **Each host spawns its own worker (goroutine)** : This makes sense since it must first read its robots.txt data, and only then proceed sequentially, one request at a time, with the specified delay between each fetch. There are no constraints inter-host, so each separate worker can crawl independently.
116* **The visitor function is called on the worker goroutine** : Again, this is ok because the crawl delay is likely bigger than the time required to parse the document, so this processing will usually *not* penalize the performance.
117* **Edge cases with no crawl delay are supported, but not optimized** : In the rare but possible event when a crawl with no delay is needed (e.g.: on your own servers, or with permission outside busy hours, etc.), gocrawl accepts a null (zero) delay, but doesn't provide optimizations. That is, there is no "special path" in the code where visitor functions are de-coupled from the worker, or where multiple workers can be launched concurrently on the same host. (In fact, if this case is your *only* use-case, I would recommend *not* to use a library at all - since there is little value in it -, and simply use Go's standard libs and fetch at will with as many goroutines as are necessary.)
118* **Extender interface provides the means to write a drop-in, fully encapsulated behaviour** : An implementation of `Extender` can radically enhance the core library, with caching, persistence, different fetching strategies, etc. This is why the `Extender.Start()` method is somewhat redundant with the `Crawler.Run()` method, `Run` allows calling the crawler as a library, while `Start` makes it possible to encapsulate the logic required to select the seed URLs into the `Extender`. The same goes for `Extender.End()` and the return value of `Run`.
119
120Although it could probably be used to crawl a massive amount of web pages (after all, this is *fetch, visit, enqueue, repeat ad nauseam*!), most realistic (and um... tested!) use-cases should be based on a well-known, well-defined limited bucket of seeds. Distributed crawling is your friend, should you need to move past this reasonable use.
121
122### Crawler
123
124The Crawler type controls the whole execution. It spawns worker goroutines and manages the URL queue. There are two helper constructors:
125
126*    **NewCrawler(Extender)** : Creates a crawler with the specified `Extender` instance.
127*    **NewCrawlerWithOptions(*Options)** : Creates a crawler with a pre-initialized `*Options` instance.
128
129The one and only public function is `Run(seeds interface{}) error` which take a seeds argument (the base URLs used to start crawling) that can be expressed a number of different ways. It ends when there are no more URLs waiting to be visited, or when the `Options.MaxVisit` number is reached. It returns an error, which is `ErrMaxVisits` if this setting is what caused the crawling to stop.
130
131<a name="types" />
132The various types that can be used to pass the seeds are the following (the same types apply for the empty interfaces in `Extender.Start(interface{}) interface{}`, `Extender.Visit(*URLContext, *http.Response, *goquery.Document) (interface{}, bool)` and in `Extender.Visited(*URLContext, interface{})`, as well as the type of the `EnqueueChan` field):
133
134*    `string` : a single URL expressed as a string
135*    `[]string` : a slice of URLs expressed as strings
136*    `*url.URL` : a pointer to a parsed URL object
137*    `[]*url.URL` : a slice of pointers to parsed URL objects
138*    `map[string]interface{}` : a map of URLs expressed as strings (for the key) and their associated state data
139*    `map[*url.URL]interface{}` : a map of URLs expressed as parsed pointers to URL objects (for the key) and their associated state data
140
141For convenience, the types `gocrawl.S` and `gocrawl.U` are provided as equivalent to the map of strings and map of URLs, respectively (so that, for example, the code can look like `gocrawl.S{"http://site.com": "some state data"}`).
142
143### Options
144
145The Options type is detailed in the next section, and it offers a single constructor, `NewOptions(Extender)`, which returns an initialized options object with defaults and the specified `Extender` implementation.
146
147### Hooks and customizations
148<a name="hc" />
149
150The `Options` type provides the hooks and customizations offered by gocrawl. All but `Extender` are optional and have working defaults.
151
152*    **UserAgent** : The user-agent string used to fetch the pages. Defaults to the Firefox 15 on Windows user-agent string.
153
154*    **RobotUserAgent** : The robot's user-agent string used to fetch and query robots.txt for permission to crawl an URL. Defaults to `Googlebot (gocrawl vM.m)` where `M.m` is the major and minor version of gocrawl. See the [robots exclusion protocol][robprot] ([full specification as interpreted by Google here][robspec]) for details about the rule-matching based on the robot's user agent. It is good practice to include contact information in the user agent should the site owner need to contact you.
155
156*    **MaxVisits** : The maximum number of pages *visited* before stopping the crawl. Probably more useful for development purposes. Note that the Crawler will send its stop signal once this number of visits is reached, but workers may be in the process of visiting other pages, so when the crawling stops, the number of pages visited will be *at least* MaxVisits, possibly more (worst case is `MaxVisits + number of active workers`). Defaults to zero, no maximum.
157
158*    **EnqueueChanBuffer** : The size of the buffer for the Enqueue channel (the channel that allows the extender to arbitrarily enqueue new URLs in the crawler). Defaults to 100.
159
160*    **HostBufferFactor** : The factor (multiplier) for the size of the workers map and the communication channel when `SameHostOnly` is set to `false`. When SameHostOnly is `true`, the Crawler knows exactly the required size (the number of different hosts based on the seed URLs), but when it is `false`, the size may grow exponentially. By default, a factor of 10 is used (size is set to 10 times the number of different hosts based on the seed URLs).
161
162*    **CrawlDelay** : The time to wait between each request to the same host. The delay starts as soon as the response is received from the host. This is a `time.Duration` type, so it can be specified with `5 * time.Second` for example (which is the default value, 5 seconds). **If a crawl delay is specified in the robots.txt file, in the group matching the robot's user-agent, by default this delay is used instead**. Crawl delay can be customized further by implementing the `ComputeDelay` extender function.
163
164*    **WorkerIdleTTL** : The idle time-to-live allowed for a worker before it is cleared (its goroutine terminated). Defaults to 10 seconds. The crawl delay is not part of idle time, this is specifically the time when the worker is available, but there are no URLs to process.
165
166*    **SameHostOnly** : Limit the URLs to enqueue only to those links targeting the same host, which is `true` by default.
167
168*    **HeadBeforeGet** : Asks the crawler to issue a HEAD request (and a subsequent `RequestGet()` extender method call) before making the eventual GET request. This is set to `false` by default. See also the `URLContext` structure explained below.
169
170*    **URLNormalizationFlags** : The flags to apply when normalizing the URL using the [purell][] library. The URLs are normalized before being enqueued and passed around to the `Extender` methods in the `URLContext` structure. Defaults to the most aggressive normalization allowed by purell, `purell.FlagsAllGreedy`.
171
172*    **LogFlags** : The level of verbosity for logging. Defaults to errors only (`LogError`). Can be a set of flags (i.e. `LogError | LogTrace`).
173
174*    **Extender** : The instance implementing the `Extender` interface. This implements the various callbacks offered by gocrawl. Must be specified when creating a `Crawler` (or when creating an `Options` to pass to `NewCrawlerWithOptions` constructor). A default extender is provided as a valid default implementation, `DefaultExtender`. It can be used by [embedding it as an anonymous field][gotalk] to implement a custom extender when not all methods need customization (see the example above).
175
176### The Extender interface
177
178This last option field, `Extender`, is crucial in using gocrawl, so here are the details for each callback function required by the `Extender` interface.
179
180*    **Start** : `Start(seeds interface{}) interface{}`. Called when `Run` is called on the crawler, with the seeds passed to `Run` as argument. It returns the data that will be used as actual seeds, so that this callback can control which seeds are processed by the crawler. See [the various supported types](#types) for more information. By default, this is a passthrough, it returns the data received as argument.
181
182*    **End** : `End(err error)`. Called when the crawling ends, with the error or nil. This same error is also returned from the `Crawler.Run()` function. By default, this method is a no-op.
183
184*    **Error** : `Error(err *CrawlError)`. Called when a crawling error occurs. Errors do **not** stop the crawling execution. A [`CrawlError`][ce] instance is passed as argument. This specialized error implementation includes - among other interesting fields - a `Kind` field that indicates the step where the error occurred, and an `*URLContext` field identifying the processed URL that caused the error. By default, this method is a no-op.
185
186*    **Log** : `Log(logFlags LogFlags, msgLevel LogFlags, msg string)`. The logging function. By default, prints to the standard error (Stderr), and outputs only the messages with a level included in the `LogFlags` option. If a custom `Log()` method is implemented, it is up to you to validate if the message should be considered, based on the level of verbosity requested (i.e. `if logFlags&msgLevel == msgLevel ...`), since the method always gets called for all messages.
187
188*    **ComputeDelay** : `ComputeDelay(host string, di *DelayInfo, lastFetch *FetchInfo) time.Duration`. Called by a worker before requesting a URL. Arguments are the host's name (the normalized form of the `*url.URL.Host`), the crawl delay information (includes delays from the Options struct, from the robots.txt, and the last used delay), and the last fetch information, so that it is possible to adapt to the current responsiveness of the host. It returns the delay to use.
189
190The remaining extension functions are all called in the context of a given URL, so their first argument is always a pointer to an `URLContext` structure. So before documenting these methods, here is an explanation of all `URLContext` fields and methods:
191
192* `HeadBeforeGet bool` : This field is initialized with the global setting from the crawler's `Options` structure. It can be overridden at any time, though to be useful it should be done before the call to `Fetch`, where the decision to make a HEAD request or not is made.
193* `State interface{}` : This field holds the arbitrary state data associated with the URL. It can be `nil` or a value of any type.
194* `URL() *url.URL` : The getter method that returns the parsed URL in non-normalized form.
195* `NormalizedURL() *url.URL` : The getter method that returns the parsed URL in normalized form.
196* `SourceURL() *url.URL` : The getter method that returns the source URL in non-normalized form. Can be `nil` for seeds or URLs enqueued via the `EnqueueChan`.
197* `NormalizedSourceURL() *url.URL` : The getter method that returns the source URL in normalized form. Can be `nil` for seeds or URLs enqueued via the `EnqueueChan`.
198* `IsRobotsURL() bool` : Indicates if the current URL is a robots.txt URL.
199
200With this out of the way, here are the other `Extender` functions:
201
202*    **Fetch** : `Fetch(ctx *URLContext, userAgent string, headRequest bool) (*http.Response, error)`. Called by a worker to request the URL. The `DefaultExtender.Fetch()` implementation uses the public `HttpClient` variable (a custom `http.Client`) to fetch the pages *without* following redirections, instead returning a special error (`ErrEnqueueRedirect`) so that the worker can enqueue the redirect-to URL. This enforces the whitelisting by the `Filter()` of every URL fetched by the crawling process. If `headRequest` is `true`, a HEAD request is made instead of a GET. Note that as of gocrawl v0.3, the default `Fetch` implementation uses the non-normalized URL.
203
204    Internally, gocrawl sets its http.Client's `CheckRedirect()` function field to a custom implementation that follows redirections for robots.txt URLs only (since a redirect on robots.txt still means that the site owner wants us to use these rules for this host). The worker is aware of the `ErrEnqueueRedirect` error, so if a non-robots.txt URL asks for a redirection, `CheckRedirect()` returns this error, and the worker recognizes this and enqueues the redirect-to URL, stopping the processing of the current URL. It is possible to provide a custom `Fetch()` implementation based on the same logic. Any `CheckRedirect()` implementation that returns a `ErrEnqueueRedirect` error will behave this way - that is, the worker will detect this error and will enqueue the redirect-to URL. See the source files ext.go and worker.go for details.
205
206    The `HttpClient` variable being public, it is possible to customize it so that it uses another `CheckRedirect()` function, or a different `Transport` object, etc. This customization should be done prior to starting the crawler. It will then be used by the default `Fetch()` implementation, or it can also be used by a custom `Fetch()` if required. Note that this client is shared by all crawlers in your application. Should you need different http clients per crawler in the same application, a custom `Fetch()` using a private `http.Client` instance should be provided.
207
208*    **RequestGet** : `RequestGet(ctx *URLContext, headRes *http.Response) bool`. Indicates if the crawler should proceed with a GET request based on the HEAD request's response. This method is only called if a HEAD was requested (based on the `*URLContext.HeadBeforeGet` field). The default implementation returns `true` if the HEAD response status code was 2xx.
209
210*    **RequestRobots** : `RequestRobots(ctx *URLContext, robotAgent string) (data []byte, request bool)`. Asks whether the robots.txt URL should be fetched. If `false` is returned as second value, the `data` value is considered to be the robots.txt cached content, and is used as such (if it is empty, it behaves as if there was no robots.txt). The `DefaultExtender.RequestRobots` implementation returns `nil, true`.
211
212*    **FetchedRobots** : `FetchedRobots(ctx *URLContext, res *http.Response)`. Called when the robots.txt URL has been fetched from the host, so that it is possible to cache its content and feed it back to future `RequestRobots()` calls. By default, this is a no-op.
213
214*    **Filter** : `Filter(ctx *URLContext, isVisited bool) bool`. Called when deciding if a URL should be enqueued for visiting. It receives the `*URLContext` and a `bool` "is visited" flag, indicating if this URL has already been visited in this crawling execution. It returns a `bool` flag ordering gocrawl to visit (`true`) or ignore (`false`) the URL. Even if the function returns `true` to enqueue the URL for visiting, the normalized form of the URL must still comply to these rules:
215
2161. It must be an absolute URL 
2172. It must have a `http/https` scheme
2183. It must have the same host if the `SameHostOnly` flag is set
219
220    The `DefaultExtender.Filter` implementation returns `true` if the URL has not been visited yet (the *visited* flag is based on the normalized version of the URLs), false otherwise.
221
222*    **Enqueued** : `Enqueued(ctx *URLContext)`. Called when a URL has been enqueued by the crawler. An enqueued URL may still be disallowed by a robots.txt policy, so it may end up *not* being fetched. By default, this method is a no-op.
223
224*    **Visit** : `Visit(ctx *URLContext, res *http.Response, doc *goquery.Document) (harvested interface{}, findLinks bool)`. Called when visiting a URL. It receives the URL context, a `*http.Response` response object, along with a ready-to-use `*goquery.Document` object (or `nil` if the response body could not be parsed). It returns the links to process (see [above](#types) for the possible types), and a `bool` flag indicating if gocrawl should find the links himself. When this flag is `true`, the `harvested` return value is ignored and gocrawl searches the goquery document for links to enqueue. When `false`, the `harvested` data is enqueued, if any. The `DefaultExtender.Visit` implementation returns `nil, true` so that links from a visited page are automatically found and processed.
225
226*    **Visited** : `Visited(ctx *URLContext, harvested interface{})`. Called after a page has been visited. The URL context and the URLs found during the visit (either by the `Visit` function or by gocrawl) are passed as argument. By default, this method is a no-op.
227
228*    **Disallowed** : `Disallowed(ctx *URLContext)`. Called when an enqueued URL gets denied acces by a robots.txt policy. By default, this method is a no-op.
229
230Finally, by convention, if a field named `EnqueueChan` with the very specific type of `chan<- interface{}` exists and is accessible on the `Extender` instance, this field will get set to the enqueue channel, which accepts [the expected types](#types) as data for URLs to enqueue. This data will then be processed by the crawler as if it had been harvested from a visit. It will trigger calls to `Filter()` and, if allowed, will get fetched and visited.
231
232The `DefaultExtender` structure has a valid `EnqueueChan` field, so if it is embedded as an anonymous field in a custom Extender structure, this structure automatically gets the `EnqueueChan` functionality.
233
234This channel can be useful to arbitrarily enqueue URLs that would otherwise not be processed by the crawling process. For example, if an URL raises a server error (status code 5xx), it could be re-enqueued in the `Error()` extender function, so that another fetch is attempted.
235
236## Thanks
237
238* Richard Penman
239* Dmitry Bondarenko
240* Markus Sonderegger
241
242## License
243
244The [BSD 3-Clause license][bsd].
245
246[bsd]: http://opensource.org/licenses/BSD-3-Clause
247[goquery]: https://github.com/PuerkitoBio/goquery
248[robots]: https://github.com/temoto/robotstxt.go
249[purell]: https://github.com/PuerkitoBio/purell
250[robprot]: http://www.robotstxt.org/robotstxt.html
251[robspec]: https://developers.google.com/webmasters/control-crawl-index/docs/robots_txt
252[godoc]: http://godoc.org/github.com/PuerkitoBio/gocrawl
253[er]: http://godoc.org/github.com/PuerkitoBio/gocrawl#EndReason
254[ce]: http://godoc.org/github.com/PuerkitoBio/gocrawl#CrawlError
255[gotalk]: http://talks.golang.org/2012/chat.slide#33
256[i10]: https://github.com/PuerkitoBio/gocrawl/issues/10
257[i9]: https://github.com/PuerkitoBio/gocrawl/issues/9
258[i14]: https://github.com/PuerkitoBio/gocrawl/issues/14