-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcrawlerhttpmorph.py
More file actions
70 lines (55 loc) · 1.53 KB
/
Copy pathcrawlerhttpmorph.py
File metadata and controls
70 lines (55 loc) · 1.53 KB
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
"""
This script is not required, RequestsCrawler can be called directly from a project.
- we just show off how it can be done
- it can be used to compare with other crawling scripts
"""
import json
import time
import argparse
import sys
from webtoolkit import (
RequestsCrawler,
PageResponseObject,
HTTP_STATUS_CODE_SERVER_ERROR,
)
from src import webtools
def get_response(link, error_text):
response = PageResponseObject(
url=link,
text=None,
status_code=HTTP_STATUS_CODE_SERVER_ERROR,
request_url=link,
)
response.add_error(error_text)
return response
def main():
webtools.WebConfig.init()
webtools.WebConfig.use_print_logging()
parser = webtools.ScriptCrawlerParser()
parser.parse()
if not parser.is_valid():
sys.exit(1)
return
request = parser.get_request()
try:
crawler = webtools.HttpMorphCrawler(request=request)
if parser.args.verbose:
print("Running request:{} with RequestsCrawler".format(request))
response = None
try:
response = crawler.run()
except Exception as E:
crawler.add_error(str(E))
try:
crawler.close()
except Exception as E:
crawler.add_error(str(E))
if not response:
response = crawler.response
if response:
parser.save(response)
return
except Exception as E:
resonse = get_response(parser.args.url, str(E))
parser.save(response)
main()