-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrawler_2.0_stable.py
152 lines (134 loc) · 4.62 KB
/
crawler_2.0_stable.py
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
#!/usr/bin/python
#########################################USAGE###############################################
#$Surface Crawler By Vishvendra Singh-Multiprocess approach #
#$crawler_2.0.py 8 http://example.com example.com #
#$crawler_2.0.py workers Full_URL mongo_collection_name #
#Details - Overload is sent to thread 1, rest of the load is distributed as equal to others #
#############################################################################################
'''START IMPORT HERE'''
import multiprocessing
import requests
import urlparse
import sys
import Queue
import lxml
from bs4 import BeautifulSoup
import re
import pymongo
import time
'''END IMPORT HERE'''
'''Do Not Change Below code'''
def getdescription(soup):
meta_desc=''
desc = soup.findAll(attrs={"name":"description"})
if len(desc):
meta_desc=desc[0]['content'].encode('utf-8')
return meta_desc
def getkeywords(soup):
meta_keyword=''
meta_key = soup.findAll(attrs={"name":"keywords"})
if len(meta_key):
meta_keyword=meta_key[0]['content'].encode('utf-8')
return meta_keyword
def gettitle(soup):
title=''
for title in soup.findAll('title'):
title=title.text
return title
def getbodytext(soup):
body=''
for elem in soup.findAll(['script', 'style']):
elem.extract()
for body in soup.findAll('body'):
body=body.text
return " ".join(body.split())
def getphone(html):
ph_list=[]
str_list=re.findall("(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{5})", html)
ph_list=str_list = filter(None, str_list)
ph_list = filter(None, ph_list)
return ph_list
def getemail(html):
email_match = re.findall(r'[\w\.-]+@[\w\.-]+', html)
return email_match
def geturl(soup):
urldict = {}
for link in soup.findAll("a"):
url_mended=urlparse.urljoin(url_callable, link.get("href"))
url_crap = urlparse.urldefrag(url_mended)
url_mended=url_crap[0]
urldict[link.text]=url_mended
return urldict
def storeData(data_dump):
try:
db[collection].insert(data_dump,check_keys=False)
except ValueError:
print "Oops! Dict 1 of functions throw this error..."
'''Do Not Change Above code'''
def worker(i,no_of_links):
for i in range(1,no_of_links+1):
print ''''''
fetched = q.get_nowait()
print "--",fetched,"--By Thread-",i,"--"
print "Fetching...",fetched
r=requests.get(fetched)
html=r.text
soup=BeautifulSoup(html,"lxml")
all_url_dict=geturl(soup)
data_dump = {'url':str(fetched),'page_title':gettitle(soup),'unix_time':time.time(),'meta_description':getdescription(soup),'meta_keywords':getkeywords(soup),'body':getbodytext(soup),'phone':getphone(html),'email':getemail(html),'all_url':all_url_dict}
try:
storeData(data_dump)
except ValueError:
print "Opps! store funciton error"
print ''''''
return
'''START CRAWLABLE URL '''
url=list(sys.argv)[2]
if url.endswith('/'):
url = url[:-1]
if urlparse.urlparse(url).scheme=='':
url='http://'+url
url_clean=urlparse.urlparse(url).netloc
url_callable=urlparse.urlparse(url).scheme+'://'+urlparse.urlparse(url).netloc
print url_callable
'''END CRAWLABLE URL '''
##mongodb authentication
#conn = pymongo.MongoClient('mongodb://username:password@hostname')
#db=conn.database
##mongodb authentication
try:
collection=list(sys.argv)[3]
except:
collection=url_clean
no_of_threads=int(list(sys.argv)[1]) #No of thread define here
q = Queue.Queue()
user_agent = {'User-agent': 'Mozilla/5.0'}
r=requests.get(url, headers = user_agent)
html=r.text
soup=BeautifulSoup(html,"lxml")
all_url_dict=geturl(soup)
all_url_list=list(all_url_dict.values())
total_links=len(all_url_list)
for item in all_url_list:
q.put(item)
print "-Queue Size is - ",q.qsize()
no_of_links_to_one_thread=q.qsize()/no_of_threads
if(no_of_links_to_one_thread<1):
no_of_links_to_one_thread=1
if(q.qsize()<no_of_threads):
no_of_thread=q.qsize()
else:
no_of_thread=no_of_threads
print no_of_thread,"--no thread "
total_left=q.qsize()
for i in range(1,no_of_thread+1):
if(i==1 and q.qsize()>no_of_threads ):
extra=total_links%no_of_threads
to_send=extra+no_of_links_to_one_thread
else:
to_send=no_of_links_to_one_thread
print to_send,"to send to thread",i
t = multiprocessing.Process(target=worker,args=(i,to_send))
t.start()
t.join()
print ''''''