-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrawl.py
More file actions
executable file
·72 lines (58 loc) · 1.97 KB
/
crawl.py
File metadata and controls
executable file
·72 lines (58 loc) · 1.97 KB
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
#!/usr/bin/env python
# monkey-patch
import gevent.monkey
gevent.monkey.patch_all()
import sys
import hashlib
import re
import requests
from requests.exceptions import ConnectionError, MissingSchema
import gevent.pool
from gevent.queue import JoinableQueue
source = sys.argv[1] #source link
num_worker_threads = int(sys.argv[2]) #specifying how many workers fetch concurrently
num_to_crawl = int(sys.argv[3]) #maximum no. of pages to fetch
crawled = 0
links_added = 0
q = JoinableQueue() #JoinableQueue lets us wait till all the tasks in the queue are marked as done.
#This function does the actual work of fetching the link and
#adding the extracted links from the page content into the queue
def do_work(link, crawler_id):
global crawled, links_added
#NOTE: uncomment this line to get extra details on what's happening
#print 'crawling', crawled, crawler_id, link
#Fetch the link
try:
response = requests.get(link)
response_content = response.content
except (ConnectionError, MissingSchema):
return
crawled += 1
#Some non-IO bound work on the content In the real world, there
#would be some heavy-duty parsing, DOM traversal here.
m = hashlib.md5()
m.update(response_content)
m.digest()
#Extract the links and add them to the queue. Using links_added
#counter to limit the number of links to fetch.
for link in re.findall('<a href="(http.*?)"', response_content):
if links_added < num_to_crawl:
links_added += 1
q.put(link)
#Worker spawned by gevent. Continously gets links, works on them and marks
#them as done.
def worker(crawler_id):
while True:
item = q.get()
try:
do_work(item, crawler_id)
finally:
q.task_done()
#Spawning worker threads.
crawler_id = 0
for i in range(num_worker_threads):
gevent.spawn(worker, crawler_id)
crawler_id += 1
q.put(source)
links_added += 1
q.join() # block until all tasks are done