-
Notifications
You must be signed in to change notification settings - Fork 6
/
crawler_base.py
276 lines (224 loc) · 9.45 KB
/
crawler_base.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
import time
import os
import re
import pickle
import json
import copy
import shutil
import constants
import traceback
from random import uniform
from tqdm import tqdm
from bs4 import BeautifulSoup
import constants
from queue import Queue, Empty
import multiprocessing
from multiprocessing.dummy import Pool, Lock
from sessions import TouchVPNSession
class SetQueue(Queue):
def _init(self, maxsize=0):
self.queue = set()
def _qsize(self):
return len(self.queue)
def _put(self, item):
self.queue.add(item)
def _get(self):
return self.queue.pop()
def __contains__(self, item):
with self.mutex:
return item in self.queue
class CrawlerBase(object):
def __init__(self, base_url, seed_links=[], out_dir='./', default_id='non-article-page',
max_concurrent_requests=constants.MAX_CONCURRENT_REQUESTS,
session_class=TouchVPNSession, *session_args, **session_kwargs):
self.base_url = base_url
self.sessions = [session_class(*session_args, **session_kwargs) for _ in range(max_concurrent_requests)]
self.session_locks = [Lock() for _ in range(max_concurrent_requests)]
self.crawler_lock = Lock()
self.process_pool = Pool(processes=max_concurrent_requests)
self.out_dir = out_dir
self.default_id = default_id
self.progress_bar = tqdm(unit='')
self.downloaded_articles = 0
self.total_links = 0
self.url_queue, self.link2id = self.load_crawler_state(seed_links)
def load_crawler_state(self, seed_links):
log_dir = os.path.join(self.out_dir, 'logs')
queue_loc = os.path.join(log_dir, 'queue_links.json')
link2id_loc = os.path.join(log_dir, 'link2id.json')
os.makedirs(os.path.join(self.out_dir, 'extracted_data'), exist_ok=True)
if constants.SAVE_RAW_HTML:
os.makedirs(os.path.join(self.out_dir, 'raw_data'), exist_ok=True)
if os.path.isdir(log_dir):
with open(queue_loc) as f:
l = json.load(f)
with open(link2id_loc) as f:
d = json.load(f)
else:
l = []
d = {}
all_links = set(l + [self.base_url] + seed_links + list(d.keys()))
q = SetQueue()
for link in all_links:
if link not in d or d[link] == -1 or d[link] == self.default_id:
d.pop(link, None)
q.put(link)
self.total_links += 1
return q, d
def save_crawler_state(self, empty_queue=True):
log_dir = os.path.join(self.out_dir, 'logs')
queue_loc = os.path.join(log_dir, 'queue_links.json')
link2id_loc = os.path.join(log_dir, 'link2id.json')
os.makedirs(log_dir, exist_ok=True)
if empty_queue:
with open(queue_loc, 'w') as f:
queue_links = self.serialize_queue()
json.dump(queue_links, f, ensure_ascii=False, indent=4)
with open(link2id_loc, 'w') as f:
json.dump(self.link2id, f, ensure_ascii=False, indent=4)
def serialize_queue(self):
elements = []
while True:
try:
element = self.url_queue.get(False)
self.url_queue.task_done()
elements.append(element)
except Empty:
return elements
def update_queue(self, elements):
self.crawler_lock.acquire()
if isinstance(elements, str):
self.url_queue.put(elements)
else: # assume elements is a non string iterable
for element in elements:
self.url_queue.put(element)
self.total_links += 1
self.crawler_lock.release()
def update(self, idx):
lock = self.session_locks[idx]
session = self.sessions[idx]
try:
links, content, output_fname = self.parse_html(session)
except:
print(traceback.format_exc())
links, content, output_fname = [], '', ''
if not links and not content and not output_fname:
try:
session.open_new_session()
except:
pass
lock.release()
return
self.save_data(session, links, content, output_fname)
lock.release()
def handle_error(self, error):
print('error_callback:', error)
def save_data(self, session, links, content, output_fname):
self.crawler_lock.acquire()
self.progress_bar.update()
self.progress_bar.set_description(f'downloaded : {self.downloaded_articles}, total links: {self.total_links}, fetched links')
if self.progress_bar.n and (self.progress_bar.n % constants.SAVE_CRAWLER_STATE_INTERVAL) == 0:
self.save_crawler_state(empty_queue=False)
if not output_fname:
output_fname = self.default_id
self.link2id[session.session.current_url] = output_fname
if content and output_fname != self.default_id:
with open(os.path.join(self.out_dir, 'extracted_data', output_fname), 'w',encoding="utf8") as f:
print(content, file=f)
if constants.SAVE_RAW_HTML:
try:
html = session.session.page_source
except:
html = ''
with open(os.path.join(self.out_dir, 'raw_data', output_fname), 'w',encoding="utf8") as f:
print(html, file=f)
self.downloaded_articles += 1
for link in links:
if link not in self.link2id:
self.url_queue.put(link)
self.total_links += 1
self.crawler_lock.release()
def parse_html(self, session):
"""Figure out what kind of page this is and return new links to crawl, extracted data and possible
output file name. If both links and content are empty, it'll be considered that something
has gone wrong and this is a signal to use a new session. If theres only one producer thread,
you could use ```update_queue(elements)``` to enter new links in the queue as soon as they
are encountered.
Args:
session ([type]): [description]
Returns:
links (List): New links to crawl
content (string): Content extracted from this page
output_fname (string): output filename if this page will be saved to disk, `None` otherwise
Raises:
NotImplementedError: [description]
"""
raise NotImplementedError()
def run(self):
req_count = 0
try:
next_url = self.url_queue.get(False)
self.url_queue.task_done()
except Empty:
next_url = False
while next_url:
try:
idx = req_count % len(self.sessions)
self.process_pool.apply_async(
self.request, args=(idx, next_url),
callback=self.update,
error_callback=self.handle_error
)
patience = 0
while True:
try:
next_url = self.url_queue.get(timeout=15)
self.url_queue.task_done()
except Empty:
time.sleep(15)
locks = [lock for lock in self.session_locks if lock.locked()]
if locks:
continue
else:
patience += 1
if patience >= 4:
raise Empty
else:
continue
self.crawler_lock.acquire()
if next_url in self.link2id:
self.crawler_lock.release()
continue
else:
self.link2id[next_url] = -1
req_count += 1
self.crawler_lock.release()
break
except Empty:
self.progress_bar.set_description(f'Exited queue loop, total links: {self.total_links}')
break
except:
print(traceback.format_exc())
self.save_crawler_state()
try:
self.wait_for_completion()
except: # in case we press ctrl-c
print(traceback.format_exc())
self.save_crawler_state()
self.progress_bar.close()
for session in self.sessions:
session.quit()
def request(self, idx, url):
lock = self.session_locks[idx]
lock.acquire()
session = self.sessions[idx]
session.request(url)
return idx
def wait_for_completion(self):
self.process_pool.close()
self.process_pool.join()
self.save_crawler_state()
self.url_queue.join()
self.progress_bar.close()
for session in self.sessions:
session.quit()