-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.py
427 lines (346 loc) · 15.7 KB
/
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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# Copyright (C) 2011 by Peter Goodman
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import urllib2
import urlparse
from BeautifulSoup import *
from collections import defaultdict
from pagerank import page_rank
import redis
import re
import threading
import time
def attr(elem, attr):
"""An html attribute from an html element. E.g. <a href="">, then
attr(elem, "href") will get the href or an empty string."""
try:
return elem[attr]
except:
return ""
WORD_SEPARATORS = re.compile(r'\s|\n|\r|\t|[^a-zA-Z0-9\-_]')
class crawler(object):
"""Represents 'Googlebot'. Populates a database by crawling and indexing
a subset of the Internet.
This crawler keeps track of font sizes and makes it simpler to manage word
ids and document ids."""
def __init__(self, db_conn, url_file):
"""Initialize the crawler with a connection to the database to populate
and with the file containing the list of seed URLs to begin indexing."""
self._url_queue = [ ]
self._doc_id_cache = { }
self._word_id_cache = { }
#dicts for id->doc/word
self._doc_cache = { }
self._word_cache = { }
#dicts for doc->word and vice-versa
self._doc_to_word = { }
self._inverted_index = { }
self._links = set()
self._scores = { }
self._word_count = { }
self._doc_title = { }
self._max_depth = 0
# functions to call when entering and exiting specific tags
self._enter = defaultdict(lambda *a, **ka: self._visit_ignore)
self._exit = defaultdict(lambda *a, **ka: self._visit_ignore)
# add a link to our graph, and indexing info to the related page
self._enter['a'] = self._visit_a
# record the currently indexed document's title an increase
# the font size
def visit_title(*args, **kargs):
self._visit_title(*args, **kargs)
self._increase_font_factor(7)(*args, **kargs)
# increase the font size when we enter these tags
self._enter['b'] = self._increase_font_factor(2)
self._enter['strong'] = self._increase_font_factor(2)
self._enter['i'] = self._increase_font_factor(1)
self._enter['em'] = self._increase_font_factor(1)
self._enter['h1'] = self._increase_font_factor(7)
self._enter['h2'] = self._increase_font_factor(6)
self._enter['h3'] = self._increase_font_factor(5)
self._enter['h4'] = self._increase_font_factor(4)
self._enter['h5'] = self._increase_font_factor(3)
self._enter['title'] = visit_title
# decrease the font size when we exit these tags
self._exit['b'] = self._increase_font_factor(-2)
self._exit['strong'] = self._increase_font_factor(-2)
self._exit['i'] = self._increase_font_factor(-1)
self._exit['em'] = self._increase_font_factor(-1)
self._exit['h1'] = self._increase_font_factor(-7)
self._exit['h2'] = self._increase_font_factor(-6)
self._exit['h3'] = self._increase_font_factor(-5)
self._exit['h4'] = self._increase_font_factor(-4)
self._exit['h5'] = self._increase_font_factor(-3)
self._exit['title'] = self._increase_font_factor(-7)
# never go in and parse these tags
self._ignored_tags = set([
'meta', 'script', 'link', 'meta', 'embed', 'iframe', 'frame',
'noscript', 'object', 'svg', 'canvas', 'applet', 'frameset',
'textarea', 'style', 'area', 'map', 'base', 'basefont', 'param',
])
# set of words to ignore
self._ignored_words = set([
'', 'the', 'of', 'at', 'on', 'in', 'is', 'it',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'and', 'or',
])
# TODO remove me in real version
self._mock_next_doc_id = 1
self._mock_next_word_id = 1
# keep track of some info about the page we are currently parsing
self._curr_depth = 0
self._curr_url = ""
self._curr_doc_id = 0
self._font_size = 0
self._curr_words = None
# get all urls into the queue
try:
with open(url_file, 'r') as f:
for line in f:
self._url_queue.append((self._fix_url(line.strip(), ""), 0))
except IOError:
pass
# TODO remove me in real version
def _mock_insert_document(self, url):
"""A function that pretends to insert a url into a document db table
and then returns that newly inserted document's id."""
ret_id = self._mock_next_doc_id
self._mock_next_doc_id += 1
return ret_id
# TODO remove me in real version
def _mock_insert_word(self, word):
"""A function that pretends to inster a word into the lexicon db table
and then returns that newly inserted word's id."""
ret_id = self._mock_next_word_id
self._mock_next_word_id += 1
return ret_id
def word_id(self, word):
"""Get the word id of some specific word."""
if word in self._word_id_cache:
return self._word_id_cache[word]
# TODO: 1) add the word to the lexicon, if that fails, then the
# word is in the lexicon
# 2) query the lexicon for the id assigned to this word,
# store it in the word id cache, and return the id.
word_id = self._mock_insert_word(word)
self._word_id_cache[word] = word_id
self._word_cache[word_id] = word
return word_id
def document_id(self, url):
"""Get the document id for some url."""
if url[-1] == '/':
url = url[:-1]
if url in self._doc_id_cache:
return self._doc_id_cache[url]
# TODO: just like word id cache, but for documents. if the document
# doesn't exist in the db then only insert the url and leave
# the rest to their defaults.
doc_id = self._mock_insert_document(url)
self._doc_id_cache[url] = doc_id
self._doc_cache[doc_id] = url
return doc_id
def _fix_url(self, curr_url, rel):
"""Given a url and either something relative to that url or another url,
get a properly parsed url."""
rel_l = rel.lower()
if rel_l.startswith("http://") or rel_l.startswith("https://"):
curr_url, rel = rel, ""
# compute the new url based on import
curr_url = urlparse.urldefrag(curr_url)[0]
parsed_url = urlparse.urlparse(curr_url)
return urlparse.urljoin(parsed_url.geturl(), rel)
def add_link(self, from_doc_id, to_doc_id):
"""Add a link into the database, or increase the number of links between
two pages in the database."""
self._links.add((from_doc_id, to_doc_id, self._curr_depth))
def _visit_title(self, elem):
"""Called when visiting the <title> tag."""
title_text = self._text_of(elem).strip()
print "document title="+ repr(title_text)
print "setting doc_title for " + str(self._curr_doc_id)
self._doc_title[self._curr_doc_id] = str(title_text)
# TODO update document title for document id self._curr_doc_id
def _visit_a(self, elem):
"""Called when visiting <a> tags."""
dest_url = self._fix_url(self._curr_url, attr(elem,"href"))
#print "href="+repr(dest_url), \
# "title="+repr(attr(elem,"title")), \
# "alt="+repr(attr(elem,"alt")), \
# "text="+repr(self._text_of(elem))
# add the just found URL to the url queue
self._url_queue.append((dest_url, self._curr_depth))
# add a link entry into the database from the current document to the
# other document
self.add_link(self._curr_doc_id, self.document_id(dest_url))
# TODO add title/alt/text to index for destination url
def _add_words_to_document(self):
# knowing self._curr_doc_id and the list of all words and their
# font sizes (in self._curr_words), add all the words into the
# database for this document
word_id_set = set()
for word_id, font_size in self._curr_words:
word_id_set.add(word_id)
self._doc_to_word[self._curr_doc_id] = word_id_set
print " num words="+ str(len(self._curr_words))
def _increase_font_factor(self, factor):
"""Increade/decrease the current font size."""
def increase_it(elem):
self._font_size += factor
return increase_it
def _visit_ignore(self, elem):
"""Ignore visiting this type of tag"""
pass
def _add_text(self, elem):
"""Add some text to the document. This records word ids and word font sizes
into the self._curr_words list for later processing."""
words = WORD_SEPARATORS.split(elem.string.lower())
for word in words:
word = word.strip().encode("ascii")
if word in self._ignored_words:
continue
self._curr_words.append((self.word_id(word), self._font_size))
def _text_of(self, elem):
"""Get the text inside some element without any tags."""
if isinstance(elem, Tag):
text = [ ]
for sub_elem in elem:
text.append(self._text_of(sub_elem))
return " ".join(text)
else:
return elem.string
def _index_document(self, soup):
"""Traverse the document in depth-first order and call functions when entering
and leaving tags. When we come accross some text, add it into the index. This
handles ignoring tags that we have no business looking at."""
class DummyTag(object):
next = False
name = ''
class NextTag(object):
def __init__(self, obj):
self.next = obj
tag = soup.html
stack = [DummyTag(), soup.html]
while tag and tag.next:
tag = tag.next
# html tag
if isinstance(tag, Tag):
if tag.parent != stack[-1]:
self._exit[stack[-1].name.lower()](stack[-1])
stack.pop()
tag_name = tag.name.lower()
# ignore this tag and everything in it
if tag_name in self._ignored_tags:
if tag.nextSibling:
tag = NextTag(tag.nextSibling)
else:
self._exit[stack[-1].name.lower()](stack[-1])
stack.pop()
tag = NextTag(tag.parent.nextSibling)
continue
# enter the tag
self._enter[tag_name](tag)
stack.append(tag)
# text (text, cdata, comments, etc.)
else:
self._add_text(tag)
def crawl_threaded(self, depth=2, timeout=3):
seen = set()
def crawl(self, depth=2, timeout=3):
"""Crawl the web!"""
seen = set()
self._max_depth = depth
start = time.time()
while len(self._url_queue):
url, depth_ = self._url_queue.pop()
# skip this url; it's too deep
if depth_ > depth:
continue
doc_id = self.document_id(url)
# we've already seen this document
if doc_id in seen:
continue
seen.add(doc_id) # mark this document as haven't been visited
socket = None
try:
socket = urllib2.urlopen(url, timeout=timeout)
soup = BeautifulSoup(socket.read())
self._curr_depth = depth_ + 1
self._curr_url = url
self._curr_doc_id = doc_id
self._font_size = 0
self._curr_words = [ ]
self._index_document(soup)
self._add_words_to_document()
print " url="+str(self._curr_url)
except Exception as e:
print e
pass
finally:
if socket:
socket.close()
mid = time.time()
self._invert_index()
self._scores = page_rank(self._links, max_depth=self._max_depth, num_iterations=20)
self._store_data()
end = time.time()
print "time spent crawling: %d" % (mid-start)
print "time spent storing: %d" % (end-mid)
#creates an inverted index of the doc_to_word dict
def _invert_index(self):
for doc, words in self._doc_to_word.iteritems():
for word in words:
if word in self._word_count:
self._word_count[word] += 1
else:
self._word_count[word] = 1
self._inverted_index.setdefault(word, set()).add(doc)
def _store_data(self):
r_server = redis.Redis(host="localhost", port=6379)
r_server.flushall()
print "doc_word_size %d" % len(self._doc_to_word)
for doc_id in self._doc_to_word:
r_server.set("doc_id:%d:doc" % doc_id, self._doc_cache[doc_id])
r_server.set("doc:%s:doc_id" % self._doc_cache[doc_id], doc_id)
r_server.set("doc_id:%d:score" % doc_id, self._scores[doc_id])
if doc_id in self._doc_title:
r_server.set("doc_id:%d:title" % doc_id, self._doc_title[doc_id])
print "word_doc_size %d" % len(self._inverted_index)
for word_id in self._inverted_index:
r_server.set("word_id:%d:word" % word_id, self._word_cache[word_id])
r_server.set("word:%s:word_id" % self._word_cache[word_id], word_id)
r_server.set("word:%s:word_count" % self._word_cache[word_id], self._word_count[word_id])
r_server.delete("word_id:%d:doc_ids" % word_id)
for doc_id in self._inverted_index[word_id]:
r_server.zadd("word_id:%d:doc_ids" % word_id, doc_id, self._scores[doc_id])
r_server.save()
def get_inverted_index(self):
return self._inverted_index
#replace inverted_index ids with the proper word strings and urls
def get_resolved_inverted_index(self):
inv = { }
for word, docs in self._inverted_index.iteritems():
for doc in docs:
inv.setdefault(self._word_cache[word], set()).add(self._doc_cache[doc])
return inv
def get_score(self, doc_id):
return self._scores[doc_id]
if __name__ == "__main__":
bot = crawler(None, "urls.txt")
bot.crawl(depth=1)