-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_crawler.py
executable file
·186 lines (151 loc) · 4.17 KB
/
email_crawler.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
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
#!/usr/bin/python
from settings import LOGGING
import logging, logging.config
import urllib, urllib2
import re, urlparse
import traceback
from database import CrawlerDb
import csv
import os
import sys
# Logging
logging.config.dictConfig(LOGGING)
log = logging.getLogger("CRAWLER_LOG")
regex_google_adurl = re.compile('adurl=(.*?)"')
regex_google_url = re.compile('url\?q=(.*?)&sa=')
regex_email = re.compile('([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})', re.IGNORECASE)
regex_url = re.compile('<a\s.*?href=[\'"](.*?)[\'"].*?>')
EMAILS_FILE = 'data/all_emails.csv'
# Set up the database
db = CrawlerDb()
db.connect()
def crawl(key,count):
log.info("-"*40)
log.info("Searhcing Keywords of %s at Google" % key.decode('utf-8'))
log.info("-"*40)
for i in range(0, count, 10):
query = {'q': key}
url = 'http://www.google.com/search?' + urllib.urlencode(query) + '&start=' + str(i)
data = get_source(url)
for url in regex_google_url.findall(data):
db.add_to_queue(unicode(url))
for url in regex_google_adurl.findall(data):
db.add_to_queue(unicode(url))
# Crawl each of the search result
while (True):
# remove_from_queue an uncrawled webpage from db
uncrawled = db.remove_from_queue()
if (uncrawled == False):
break
email_set = find_emails_subsite(uncrawled.url)
if (len(email_set) > 0):
db.crawled(uncrawled, ",".join(list(email_set)))
else:
db.crawled(uncrawled, None)
def get_source(url):
req = urllib2.Request(url)
req.add_header('User-Agent', 'Just-Crawling 0.1')
request = None
status = 0
try:
log.info("Crawling %s" % url)
request = urllib2.urlopen(req)
except urllib2.URLError, e:
log.error("Exception at url: %s\n%s" % (url, e))
except urllib2.HTTPError, e:
status = e.code
except Exception, e:
return
if status == 0:
status = 200
try:
source = request.read()
except Exception, e:
return
return str(source)
def find_emails_subsite(url):
source = get_source(url)
email_set = find_emails_in_source(source)
if (len(email_set) > 0):
return email_set
else:
# Crawl subsites
log.info('No email at site.. proceeding to crawl subsites')
links = find_links(url, source)
for link in links:
source = get_source(link)
if (source == None):
continue
email_set = find_emails_in_source(source)
db.add_to_queue(link, list(email_set))
# We return an empty set
return set()
def find_emails_in_source(source):
if (source == None):
return set()
email_set = set()
for email in regex_email.findall(source):
email_set.add(email)
return email_set
def find_links(url, source):
if (source == None):
return set()
url = urlparse.urlparse(url)
links = regex_url.findall(source)
link_set = set()
for link in links:
if link == None:
continue
try:
link = str(link)
if link.startswith("/"):
link_set.add('http://'+url.netloc+link)
elif link.startswith("http") or link.startswith("https"):
if (link.find(url.netloc)):
link_set.add(link)
elif link.startswith("#"):
continue
else:
link_set.add(urlparse.urljoin(url.geturl(),link))
except Exception, e:
pass
return link_set
def sort():
path = '~/python-email-crawler-master/data/all_emails.csv'
output = open('emails.csv', 'wb')
writer = csv.writer(output)
with open(path, "r") as csvfile:
csv_reader = csv.reader(csvfile, delimiter='\n')
for row in csv_reader:
a=row
if (not str(a).endswith("png']")):
writer.writerow(row)
def print_emails():
path = '~/python-email-crawler-master/emails.csv'
with open(path,'r') as f:
for line in f:
print line
try:
arg = sys.argv[1].lower()
if (arg == '--emails') or (arg == '-e'):
# Get all the emails and save in a CSV
log.info("="*40)
log.info("Processing...")
emails = db.get_all_emails()
log.info("There are %d emails" % len(emails))
file = open(EMAILS_FILE, "w+")
file.writelines("\n".join(emails))
file.close()
log.info("All emails saved to ./data/emails.csv")
log.info("="*40)
sort()
print_emails()
else:
count = int(sys.argv[2])
crawl(arg,count)
except KeyboardInterrupt:
log.error("KeyboardInterrupt")
sys.exit()
except Exception, e:
log.error("EXCEPTION: %s " % e)
traceback.print_exc()