forked from gocolly/colly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolly.go
377 lines (342 loc) · 9.97 KB
/
colly.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Package colly implements a HTTP scraping framework
package colly
import (
"bytes"
"errors"
"net/http"
"net/url"
"strings"
"sync"
"time"
"golang.org/x/net/html"
"github.com/PuerkitoBio/goquery"
)
// Collector provides the scraper instance for a scraping job
type Collector struct {
// UserAgent is the User-Agent string used by HTTP requests
UserAgent string
// MaxDepth limits the recursion depth of visited URLs.
// Set it to 0 for infinite recursion (default).
MaxDepth int
// AllowedDomains is a domain whitelist.
// Leave it blank to allow any domains to be visited
AllowedDomains []string
// AllowURLRevisit allows multiple downloads of the same URL
AllowURLRevisit bool
// MaxBodySize limits the retrieved response body. `0` means unlimited.
// The default value for MaxBodySize is 10240 (10MB)
MaxBodySize int
visitedURLs []string
htmlCallbacks map[string]HTMLCallback
requestCallbacks []RequestCallback
responseCallbacks []ResponseCallback
backend *httpBackend
wg *sync.WaitGroup
lock *sync.Mutex
}
// Request is the representation of a HTTP request made by a Collector
type Request struct {
// URL is the parsed URL of the HTTP request
URL *url.URL
// Headers contains the Request's HTTP headers
Headers *http.Header
// Ctx is a context between a Request and a Response
Ctx *Context
// Depth is the number of the parents of this request
Depth int
collector *Collector
}
// Response is the representation of a HTTP response made by a Collector
type Response struct {
// StatusCode is the status code of the Response
StatusCode int
// Body is the content of the Response
Body []byte
// Ctx is a context between a Request and a Response
Ctx *Context
// Request is the Request object of the response
Request *Request
// Headers contains the Response's HTTP headers
Headers *http.Header
}
// HTMLElement is the representation of a HTML tag.
type HTMLElement struct {
// Name is the name of the tag
Name string
Text string
attributes []html.Attribute
// Request is the request object of the element's HTML document
Request *Request
// Response is the Response object of the element's HTML document
Response *Response
// DOM is the goquery parsed DOM object of the page. DOM is relative
// to the current HTMLElement
DOM *goquery.Selection
}
// Context provides a tiny layer for passing data between callbacks
type Context struct {
contextMap map[string]string
lock *sync.Mutex
}
// RequestCallback is a type alias for OnRequest callback functions
type RequestCallback func(*Request)
// ResponseCallback is a type alias for OnResponse callback functions
type ResponseCallback func(*Response)
// HTMLCallback is a type alias for OnHTML callback functions
type HTMLCallback func(*HTMLElement)
// NewCollector creates a new Collector instance with default configuration
func NewCollector() *Collector {
c := &Collector{}
c.Init()
return c
}
// NewContext initializes a new Context instance
func NewContext() *Context {
return &Context{
contextMap: make(map[string]string),
lock: &sync.Mutex{},
}
}
// Init initializes the Collector's private variables and sets default
// configuration for the Collector
func (c *Collector) Init() {
c.UserAgent = "colly - https://github.com/asciimoo/colly"
c.MaxDepth = 0
c.visitedURLs = make([]string, 0, 8)
c.htmlCallbacks = make(map[string]HTMLCallback, 0)
c.requestCallbacks = make([]RequestCallback, 0, 8)
c.responseCallbacks = make([]ResponseCallback, 0, 8)
c.MaxBodySize = 10240
c.backend = &httpBackend{}
c.backend.Init()
c.wg = &sync.WaitGroup{}
c.lock = &sync.Mutex{}
}
// Visit starts Collector's collecting job by creating a
// request to the URL specified in parameter.
// Visit also calls the previously provided OnRequest,
// OnResponse, OnHTML callbacks
func (c *Collector) Visit(URL string) error {
return c.scrape(URL, "GET", 1, nil)
}
// Post starts collecting job by creating a POST
// request.
// Post also calls the previously provided OnRequest,
// OnResponse, OnHTML callbacks
func (c *Collector) Post(URL string, requestData map[string]string) error {
return c.scrape(URL, "POST", 1, requestData)
}
func (c *Collector) scrape(u, method string, depth int, requestData map[string]string) error {
c.wg.Add(1)
defer c.wg.Done()
if u == "" {
return errors.New("Missing URL")
}
if c.MaxDepth > 0 && c.MaxDepth < depth {
return errors.New("Max depth limit reached")
}
if !c.AllowURLRevisit {
visited := false
for _, u2 := range c.visitedURLs {
if u2 == u {
visited = true
break
}
}
if visited {
return errors.New("URL already visited")
}
}
parsedURL, err := url.Parse(u)
if err != nil {
return err
}
allowed := false
if c.AllowedDomains == nil || len(c.AllowedDomains) == 0 {
allowed = true
} else {
for _, d := range c.AllowedDomains {
if d == parsedURL.Host {
allowed = true
break
}
}
}
if !allowed {
return errors.New("Forbidden domain")
}
if !c.AllowURLRevisit {
c.lock.Lock()
c.visitedURLs = append(c.visitedURLs, u)
c.lock.Unlock()
}
var form url.Values
if method == "POST" {
form = url.Values{}
for k, v := range requestData {
form.Add(k, v)
}
}
req, err := http.NewRequest(method, u, strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("User-Agent", c.UserAgent)
if method == "POST" {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
ctx := NewContext()
request := &Request{
URL: parsedURL,
Headers: &req.Header,
Ctx: ctx,
Depth: depth,
collector: c,
}
if len(c.requestCallbacks) > 0 {
c.handleOnRequest(request)
}
response, err := c.backend.Do(req, c.MaxBodySize)
// TODO add OnError callback to handle these cases
if err != nil {
return err
}
response.Ctx = ctx
response.Request = request
if strings.Index(strings.ToLower(response.Headers.Get("Content-Type")), "html") > -1 {
c.handleOnHTML(response.Body, request, response)
}
if len(c.responseCallbacks) > 0 {
c.handleOnResponse(response)
}
return nil
}
// Wait returns when the collector jobs are finished
func (c *Collector) Wait() {
c.wg.Wait()
}
// OnRequest registers a function. Function will be executed on every
// request made by the Collector
func (c *Collector) OnRequest(f RequestCallback) {
c.lock.Lock()
c.requestCallbacks = append(c.requestCallbacks, f)
c.lock.Unlock()
}
// OnResponse registers a function. Function will be executed on every response
func (c *Collector) OnResponse(f ResponseCallback) {
c.lock.Lock()
c.responseCallbacks = append(c.responseCallbacks, f)
c.lock.Unlock()
}
// OnHTML registers a function. Function will be executed on every HTML
// element matched by the `goquerySelector` parameter.
// `goquerySelector` is a selector used by https://github.com/PuerkitoBio/goquery
func (c *Collector) OnHTML(goquerySelector string, f HTMLCallback) {
c.lock.Lock()
c.htmlCallbacks[goquerySelector] = f
c.lock.Unlock()
}
// WithTransport allows you to set a custom http.Transport for this collector.
func (c *Collector) WithTransport(transport *http.Transport) {
c.backend.Client.Transport = transport
}
// DisableCookies turns off cookie handling for this collector
func (c *Collector) DisableCookies() {
c.backend.Client.Jar = nil
}
// SetRequestTimeout overrides the default timeout (10 seconds) for this collector
func (c *Collector) SetRequestTimeout(timeout time.Duration) {
c.backend.Client.Timeout = timeout
}
func (c *Collector) handleOnRequest(r *Request) {
for _, f := range c.requestCallbacks {
f(r)
}
}
func (c *Collector) handleOnResponse(r *Response) {
for _, f := range c.responseCallbacks {
f(r)
}
}
func (c *Collector) handleOnHTML(body []byte, req *Request, resp *Response) {
doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(body))
if err != nil {
return
}
for expr, f := range c.htmlCallbacks {
doc.Find(expr).Each(func(i int, s *goquery.Selection) {
for _, n := range s.Nodes {
e := &HTMLElement{
Name: n.Data,
Request: req,
Response: resp,
Text: goquery.NewDocumentFromNode(n).Text(),
DOM: s,
attributes: n.Attr,
}
f(e)
}
})
}
}
// Limit adds a new `LimitRule` to the collector
func (c *Collector) Limit(rule *LimitRule) error {
return c.backend.Limit(rule)
}
// Limits adds new `LimitRule`s to the collector
func (c *Collector) Limits(rules []*LimitRule) error {
return c.backend.Limits(rules)
}
// Attr returns the selected attribute of a HTMLElement or empty string
// if no attribute found
func (h *HTMLElement) Attr(k string) string {
for _, a := range h.attributes {
if a.Key == k {
return a.Val
}
}
return ""
}
// AbsoluteURL returns with the resolved absolute URL of an URL chunk.
// AbsoluteURL returns empty string if the URL chunk is a fragment or
// could not be parsed
func (r *Request) AbsoluteURL(u string) string {
if strings.HasPrefix(u, "#") {
return ""
}
absURL, err := r.URL.Parse(u)
if err != nil {
return ""
}
absURL.Fragment = ""
if absURL.Scheme == "//" {
absURL.Scheme = r.URL.Scheme
}
return absURL.String()
}
// Visit continues Collector's collecting job by creating a
// request to the URL specified in parameter.
// Visit also calls the previously provided OnRequest,
// OnResponse, OnHTML callbacks
func (r *Request) Visit(URL string) error {
return r.collector.scrape(r.AbsoluteURL(URL), "GET", r.Depth+1, nil)
}
// Post continues a collector job by creating a POST request.
// Post also calls the previously provided OnRequest, OnResponse, OnHTML callbacks
func (r *Request) Post(URL string, requestData map[string]string) error {
return r.collector.scrape(r.AbsoluteURL(URL), "POST", r.Depth+1, requestData)
}
// Put stores a value in Context
func (c *Context) Put(key, value string) {
c.lock.Lock()
c.contextMap[key] = value
c.lock.Unlock()
}
// Get retrieves a value from Context. If no value found for `k`
// Get returns an empty string if key not found
func (c *Context) Get(key string) string {
if v, ok := c.contextMap[key]; ok {
return v
}
return ""
}