-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrawler.py
358 lines (285 loc) · 11.8 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
import datetime
import hashlib
import logging
import re
import time
import os
import requests
from threading import Lock, Thread
from queue import Queue, Empty
output_dir = 'fetched_data_new'
class URLFrontier(object):
"""
Container that keeps track of URLs we should visit in the future and URLs we have already visited.
"""
seed_urls = {
"wiki/New_York",
"wiki/Hillary_Clinton",
"wiki/Water",
"wiki/Main_Page",
"wiki/Technology",
}
def __init__(self):
# The set of visited urls.
self._visited_count = 0
self._visited_lock = Lock()
# Log all urls
self._backqueue_logger = logging.getLogger('backqueue_size')
# The set of all urls that are either visited or todo.
self._all_urls_hashes = set()
self._all_urls_lock = Lock()
# The queue that contains the urls we have to visit in the future.
self._urls = Queue()
for seed_url in URLFrontier.seed_urls:
self._urls.put(seed_url)
self._wiki_url_filters = [
re.compile('/File:'),
re.compile('/Book:'),
re.compile('/Portal:'),
re.compile('/Help:'),
re.compile('/Talk:'),
re.compile('/Wikipedia:'),
re.compile('/Wikipedia_talk:'),
re.compile('/Special:'),
re.compile('/Template:'),
re.compile('/Template_talk:'),
re.compile('/User:'),
re.compile('/User_talk:'),
re.compile('/Category:'),
re.compile('/Lists_of_'),
re.compile('/List_of_'),
re.compile('/Timeline_of_'),
re.compile('/History_of_'),
re.compile('/films_of_'),
]
# The maximum number of urls to hold in memory. Must be larger than 100000
self._url_threshold = 200000
def _clean_url(self, url):
"""
Clean a URL. Removes anchors.
:param url: The url to clean
:return: The cleaned url
"""
index = len(url)
try:
index = url.index('#')
except ValueError:
pass
return url[:index]
def add_url(self, url):
"""
Add a URL to the frontier. If the URL was already visited, it will not be added.
:param url: The URL to add
"""
# Cleanup the url first.
url = self._clean_url(url)
if self.valid_wiki_url(url):
self._all_urls_lock.acquire()
# Compute the hash of the url. We only use the first 15 elements to save some more space.
url_hash = hashlib.md5(url.encode('UTF-8')).hexdigest()
url_hash = url_hash[:15]
if url_hash not in self._all_urls_hashes:
self._all_urls_hashes.add(url_hash)
self._urls.put(url)
self._all_urls_lock.release()
# Check if we have too many URLs in memory. If yes, write them to a file for later retrieval.
if self._urls.qsize() > self._url_threshold:
self._all_urls_lock.acquire()
with open(output_dir + '/temp_urls.txt', 'a') as f:
for i in range(0, self._url_threshold - 100000):
try:
f.write(self._urls.get(False) + "\n")
except Empty:
pass
self._all_urls_lock.release()
def valid_wiki_url(self, url):
"""
Check if the given url is a valid url of an article on wikipedia.
:param url: The url that needs to be checked if it is valid.
"""
for regex in self._wiki_url_filters:
if regex.search(url) is not None:
return False
return True
def get_url(self):
"""
Get a URL that was not yet visited.
This method will block for at most 5 seconds if no URL is available. After 5 seconds it will return None.
:return: A URL (string)
"""
print("Visited: %d | Todo: %d | Total: %d" % (self._visited_count, self._urls.qsize(), len(self._all_urls_hashes)), end="\r")
try:
url = self._urls.get(timeout=5)
self._visited_lock.acquire()
self._visited_count += 1
self._visited_lock.release()
# Log the size of the back queue (# of visited and total number)
self._backqueue_logger.info("%d\t%d", self._visited_count, len(self._all_urls_hashes))
return 'https://en.wikipedia.org/' + url
except Empty as e:
# Maybe we have none left in the queue. Let's load some from disk.
self._all_urls_lock.acquire()
path = output_dir + '/temp_urls.txt'
if not os.path.isfile(path):
return None
# Load from the file
with open(path, 'r') as f:
i = 0
for line in f:
self._urls.put(line)
# Read a maximum number of lines
if i >= self._url_threshold - 100000:
break
# Check if we read the complete file.
if i < self._url_threshold - 100000:
os.remove(path)
self._all_urls_lock.release()
# We should now have something in the queue. And if not, the recursive call will return None after
# finding out that the temp_urls file doesn't exist.
return self.get_url()
class Parser(object):
"""
Parses the content of one page.
This parser does two things. It extracts URLs and the first paragraph (abstract).
The URLs are added to the URLFrontier and the first paragraph is written to disk.
"""
def __init__(self, url_frontier):
"""
:type url_frontier: URLFrontier
"""
self._url_frontier = url_frontier
# Used to count the number of pages stored. Each page will be stored with this number as its name.
self._page_counter = 0
self._page_counter_lock = Lock()
# The queue that contains (url, content) tuples that need to be processed.
self._content_queue = Queue()
# Start several threads that actually do the work.
thread_count = 3
self._threads = set()
for i in range(0, thread_count):
thread = ParserThread(i, self._content_queue, self._url_frontier)
thread.start()
self._threads.add(thread)
def parse(self, page_content, page_url):
"""
Parses a page. This method returns immediately as it parses the contents asynchronously.
:param page_url: The URL from which this content was fetched.
:param page_content: The HTML content of the page that needs to be parsed.
"""
# Ensure content queue doesn't blow up!
if self._content_queue.qsize() > 300:
time.sleep(0.1)
# Add to the queue and return immediately
self._content_queue.put((page_url, page_content))
class ParserThread(Thread):
"""
Thread that processes (page_url, page_content) tuples.
"""
def __init__(self, thread_number, content_queue, url_frontier):
super().__init__()
self._overview_logger = logging.getLogger('overview')
# The queue from which the content is fetched.
self._content_queue = content_queue
# URL Frontier to which new urls are added.
self._url_frontier = url_frontier
# Thread number together with page_counter identify a page when it is written to a file.
self._thread_number = thread_number
self._page_counter = 0
# Several regexes
self._url_regex = re.compile('href="/wiki/.*?"')
self._first_paragraph_regex = re.compile('<p>(.*?)</p>')
def run(self):
while True:
(page_url, page_content) = self._content_queue.get()
# Find all the links
li = self._url_regex.findall(page_content)
for s in li:
url = s[7:-1] # url looks like: 'wiki/...'
self._url_frontier.add_url(url)
# Save only the content of the first paragraph.
self._save_first_paragraph(page_content, page_url)
def _save_first_paragraph(self, page_content, page_url):
# Add the line to the overview.
self._overview_logger.info("%d-%d\t%s\n", self._thread_number, self._page_counter, page_url)
# Extract the first paragraph
result = self._first_paragraph_regex.search(page_content)
if result is not None:
first_paragraph_content = result.group(0)
# Make sure we ignore the "may/can refer to:" pages.
if " refer to:" not in first_paragraph_content.lower():
with open(output_dir + '/%d-%d.html' % (self._thread_number, self._page_counter), 'w') as f:
f.write(first_paragraph_content)
self._page_counter += 1
class Fetcher(object):
"""
The heart of the Crawler. Fetches data from urls obtained from the URL Frontier and forwards the content to the
parser.
"""
def __init__(self, thread_count):
"""
:type thread_count: int The number of threads to use for fetching.
"""
self._thread_count = thread_count
self._logger = logging.getLogger('processed_urls')
self._network_logger = logging.getLogger('network_log')
# Create all the objects we need.
self._url_frontier = URLFrontier()
self._parser = Parser(self._url_frontier)
def start(self):
# Create the threads and start them.
threads = []
for i in range(0, self._thread_count):
thread = Thread(target=Fetcher._fetch_pages, args=(self._url_frontier, self._parser, self._logger, self._network_logger))
thread.start()
threads.append(thread)
# Let's wait for the threads to finish...
for t in threads:
t.join()
print("Finished Crawling at %s" % datetime.datetime.now())
@staticmethod
def _fetch_pages(url_frontier, parser, logger, network_logger):
"""
Method fetches pages as long as url_frontier returns not visited pages.
:type parser: Parser
:type url_frontier: URLFrontier
:param url_frontier: The URL frontier used to get new pages.
:param parser: The parser to which the the page content should be sent.
:return: None
"""
# We work with a session to reuse TCP connections.
session = requests.Session()
while True:
url = url_frontier.get_url()
# Finish this method if we can't get any more urls...
if url is None:
return
# Get that content.
logger.info(url)
# Try to get the content of that url and parse it.
try:
start = time.time()
response = session.get(url)
network_logger.info("%f", time.time() - start)
parser.parse(response.text, response.url)
except ConnectionError:
print("Failed to get URL: %s" % url)
def setup_logging():
loggers = [
("overview", "0_overview.log"), # Number and URL
("processed_urls", "0_processed_urls.log"), # All URLs that have been processed so far
("backqueue_size", "0_backqueue_size.log"), # Number and URL
("network_log", "0_network_log.log"), # Time to fetch pages
]
for (name, filename) in loggers:
l = logging.getLogger(name)
l.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s : %(message)s')
file_handler = logging.FileHandler(filename=output_dir + '/' + filename, mode='w')
file_handler.setFormatter(formatter)
l.addHandler(file_handler)
# Make this file callable!
if __name__ == "__main__":
setup_logging()
# Start fetching data!
fetcher = Fetcher(10)
fetcher.start()