-
Notifications
You must be signed in to change notification settings - Fork 3
/
mini_spider.py
executable file
·337 lines (271 loc) · 11.9 KB
/
mini_spider.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
# Copyright (c) 2017, Lei Wang
# All rights reserved.
# Author yiak.wy@gmail.com
# Updated on Dec 25, 2020 by Lei
## system libraries
import sys
import os
from os.path import join, abspath, realpath, dirname
ROOT = dirname(realpath(__file__))
sys.path.insert(0, ROOT)
from config import settings, get_conf
import argparse
setattr(settings, "ROOT", ROOT)
# sys.path.insert(0, "%s/core" % ROOT)
# sys.path.insert(0, "%s/lib" % ROOT)
# sys.path.insert(0, "%s/utils" % ROOT)
# sys.path.insert(0, "%s/cloud" % ROOT)
# sys.path.insert(0, "%s/cmds" % ROOT)
# sys.path.insert(0, "%s/config" % ROOT)
# sys.path.insert(0, "%s/models" % ROOT)
from io import StringIO
import logging
# urlparse utility
try:
from urllib.parse import urlparse, urlencode, quote_plus, parse_qsl
except:
# py2
from urlparse import urlparse, urlencode, quote_plus, parse_qsl
## third party libraries
from lxml import etree
## customer libraries
from core.downloader.handlers.async_socket_http11 import Task, SimpleEventLoop, Queue
from core.spiders.base_spider import BaseAsyncSpider
import utils.Log as Log
_logger = logging.getLogger("spiders")
from config import settings
DEBUG_ETREE = True
class MyAsyncSpider(BaseAsyncSpider):
"""
Implementation Code of Spider Services. This module demonstrates how to use `BaseAsyncSpider` in business side.
Example:
see examples from `fetch_imgs_from_root` and `fetch_imgs_from_urls` in this module
Key members to implement:
parse_links(response : Response)
"""
logger = Log.LogAdapter(_logger, "MyAsyncSpider")
# sitemap relevant, designed for "konachan.net"
Rules_linked_websites = [
"//div[@class='paginator']/a/@href",
"//ul[@id='tag-sidebar']/li/a[2]/@href"
]
Rules_for_images_media_type = [
"//img[@class='preview']/@src",
]
# extend your customer links patten here
Rules_your_general_rule = [
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
:param target_number: maximum of number of links to be crawled
:param count: used by BaseAsyncSpider to count the number of links crawled
:param dirname : the directory name to store media resources
:param _is_media_type_to_be_downloaded : whether to download media types
:param _media_types_to_be_downloaded : media types to be downloaded
"""
self.target_number = 1000000000
self.count = 0
self.dirname = "../images"
self._is_media_type_to_be_downloaded = True
self._media_types_to_be_downloaded = ["jpg", ]
def parse_links(self, response):
"""
:param response: Response, similiar to HTTPResponse, defined async_socket_http11 downloader
:return: List<str>, parsed urls from document object and return to BaseAsyncSpider which defined crawler algorithms
to be used.
"""
# import pdb; pdb.set_trace()
parser = etree.HTMLParser()
body = response.body.decode(response.headers.get_content_charset() or "utf-8")
tree = etree.parse(StringIO(body), parser=parser)
if DEBUG_ETREE:
if etree.tostring(tree) is None:
raise Exception("Bad Values!")
urls = []
hostname = self.cur_addr.hostname
# process relative path
def _process_extracted_path(path):
parsed = urlparse(path)
hostname = self.cur_addr.hostname
scheme = self.cur_addr.scheme
if parsed.hostname is not None and parsed.hostname is not "":
hostname = parsed.hostname
if parsed.scheme is not None and parsed.scheme is not "":
scheme = parsed.scheme
kw = dict(parse_qsl(parsed.query))
parsed_path = parsed.path if len(kw) == 0 else "{}?{}".format(parsed.path, urlencode(kw))
url = "%s://%s/%s" % (scheme, hostname, parsed_path[1:])
return url
if hostname == "konachan.net":
for rule in self.Rules_for_images_media_type:
try:
for path in tree.xpath(rule):
url = _process_extracted_path(path)
yield url
urls.append(url)
except Exception as e:
print(e)
for rule in self.Rules_linked_websites:
for path in tree.xpath(rule):
# the value will be sent to outer layer
url = _process_extracted_path(path)
yield url
urls.append(url)
for rule in self.Rules_your_general_rule:
for path in tree.xpath(rule):
url = _process_extracted_path(path)
yield url
urls.append(url)
# @todo TODO dump the parsed urls to files
return urls
def fetch_imgs_from_root(root_url):
"""
:param root_url: str, parsed root url the crawler starting from
:return: None, produced data and files are processed internally by base crawlers defined in core/spiders/base_spider.py
"""
Log.InitLogFrmConfig(settings.LOGGING)
loop = SimpleEventLoop()
loop.set_timeout(settings.TIME_OUT)
# Note I just freshly added support to HTTPS
spider = MyAsyncSpider(root_url=root_url, max_redirect=3, max_depth=10, loop=loop)
def routine(concurrency):
tasks = [Task(spider._run()) for _ in range(concurrency)]
yield from spider._q.join()
for t in tasks:
t.cancel()
loop.run_until_complete(routine(1000))
def fetch_imgs_from_urls(urls):
"""
:param urls: List<str>, a list of parsed urls
:return: None, produced data and files are processed internally by base crawlers defined in core/spiders/base_spider.py
"""
Log.InitLogFrmConfig(settings.LOGGING)
loop = SimpleEventLoop()
loop.set_timeout(settings.TIME_OUT)
q = Queue(maxsize=10000)
def routine(urls):
tasks = []
for url in urls:
spider = MyAsyncSpider(root_url=url, max_redirect=3, max_depth=settings.MAX_DEPTH, loop=loop, queue=q)
# configure attributes parsed from spider.conf
task = Task(spider._run())
tasks.append(task)
yield from q.join()
for t in tasks:
t.cancel()
loop.run_until_complete(routine(urls))
def shell(raw_args):
usage = """
mini.py [--<opt>]
-c : spider configuration file
-v : show software version
"""
__version__ = (0,1,0)
parser = argparse.ArgumentParser(description=__doc__, usage=usage)
parser.add_argument('-c', '--conf', help="")
parser.add_argument('-v', '--version', action='version', version='.'.join(map(lambda x:str(x), __version__)))
parser.add_argument('argc', nargs='?', type=int)
parser.add_argument('argv', nargs=argparse.REMAINDER, help="arguments for sub command")
args = parser.parse_args(raw_args)
if args.conf:
print("reading %s ... " % args.conf)
conf_path = os.path.join(ROOT, args.conf)
conf_obj = get_conf(conf_path)
if conf_obj.get('spider', None) is not None:
url_list_file = conf_obj['spider'].get('url_list_file', None)
output_directory = conf_obj['spider'].get('output_directory', None)
max_depth = conf_obj['spider'].get('max_depth', None)
crawl_interval = conf_obj['spider'].get('crawl_interval', None)
crawl_timeout = conf_obj['spider'].get('crawl_timeout', None)
target_url = conf_obj['spider'].get('target_url', None)
def is_valid_fn(fn):
if fn is not None and fn is not "":
if os.path.exists(fn) and os.path.isfile(fn):
return True
else:
logging.error("%s is not a valid file" % fn)
return False
logging.error("fn is an empty string or None!")
return False
def is_dir(path):
if path is not None and path is not "":
if os.path.exists(path) and os.path.isdir(path):
return True
else:
logging.warning("%s is not a valid directory" % path)
return False
logging.error("path is an empty string or None!")
return False
def parse_url(field):
if field is not None and field is not "":
import re
# see https://github.com/django/django/blob/master/django/core/validators.py#L74
# also see
# https://stackoverflow.com/questions/827557/how-do-you-validate-a-url-with-a-regular-expression-in-python
url_regex = re.compile(r"""((http|https)\:\/\/)? # scheme
[a-zA-Z0-9\.\/\?\: # port
@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])* # path""",
re.IGNORECASE | re.VERBOSE)
searched = url_regex.search(field)
if searched is not None:
return searched.string
else:
logging.error("URL path <%s> is not a valid url" % field)
return None
logging.error("field is an empty string or None!")
return None
def parse_number(field):
if field is not None:
try:
val = int(field)
except ValueError:
logging.error("%s is not a number" % field)
return None
return val
logging.error("field is None!")
return None
urls = []
if is_valid_fn(url_list_file):
with open(url_list_file, 'r') as f:
for line in f.readlines():
url = parse_url(line.strip())
if url is not None:
urls.append(url)
if is_dir(output_directory):
setattr(settings, "output_directory".upper(), output_directory)
else:
os.system("mkdir -p %s" % output_directory)
setattr(settings, "output_directory".upper(), "./log/output")
max_depth = parse_number(max_depth)
if max_depth is not None:
setattr(settings, "max_depth".upper(), max_depth)
else:
setattr(settings, "max_depth".upper(), 10)
crawl_interval = parse_number(crawl_interval)
if crawl_interval is not None:
setattr(settings, "crawl_interval".upper(), crawl_interval)
else:
setattr(settings, "crawl_interval".upper(), -1) # no throttling strategy configured
crawl_timeout = parse_number(crawl_timeout)
if crawl_interval is not None:
settings.TIME_OUT = crawl_timeout
if target_url is not None and target_url is not "":
# is a valid regex
import re
target_url_regex = re.compile(target_url, re.IGNORECASE)
setattr(settings, "target_url_regex".upper(), target_url_regex)
fetch_imgs_from_urls(urls)
# older configuration file format, see spider.conf.bak
elif conf_obj.get('ROOT_URL', None) is not None:
root_url = conf_obj['ROOT_URL'].get("ROOT_URL".lower(), None)
if root_url is None:
raise(Exception("Wrong input! option <ROOT_URL> in section 'ROOT_URL' is required!"))
fetch_imgs_from_root(root_url)
else:
raise Exception("Not supported for the moment, pull requests are welcome!")
else:
print("Not valid input!")
parser.print_help()
if __name__ == "__main__":
sys.exit(shell(sys.argv[1:]))