-
Notifications
You must be signed in to change notification settings - Fork 1
/
spider.py
181 lines (157 loc) · 6.22 KB
/
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
import requests
import multiprocessing
from randomHP import random_header, random_proxy
from redis import StrictRedis
import myRedis
import re
from urllib import parse
from lxml import html
import multiprocessing as mp
import logging
import coloredlogs
import traceback
from config import SPIDER_PROCESS_NUM, SPIDER_NAME
log = logging.getLogger(__name__)
coloredlogs.install(
logger=log,
level="DEBUG",
fmt="[%(levelname)s] %(message)s"
)
class Spider:
def check_if_login_form(self, content):
if len(re.findall(
r"type[ ]*=[ ]*['\"]password['\"]",
content, flags=re.IGNORECASE | re.MULTILINE
)) != 0:
return True
else:
return False
def simple_request(self, url):
log.info("[+] now crawing: %s" % url)
try:
r = requests.get(
url, headers=random_header(),
timeout=5, proxies={}
)
#del from todo
try:
redis_manager.del_todo(url)
log.info("success [+] delete from todo")
except Exception as e:
log.exception("error [-] when delete from todo")
redis_manager.add_error(url)
if r.status_code == 200:
try:
if self.check_if_login_form(str(r.content)):
log.warning("[+] login form found! " + url)
try:
redis_manager.add_login_form(url)
log.info("success [+] add to login form")
except Exception as e:
log.exception("error [-] when add to login form")
redis_manager.add_error(url)
else:
log.warning("[-] login form not found")
try:
redis_manager.add_finish(url)
log.info("success [+] add to finish")
except Exception as e:
log.exception("error [-] when add to finish")
redis_manager.add_error(url)
return str(r.content)
except Exception as e:
log.exception("error [-] when check if login form")
redis_manager.add_error(url)
else:
log.info(str(r.status_code) + " [+] " + str(url))
#add to forbidden
try:
redis_manager.add_forbidden(url)
log.warning("success [+] add to forbidden")
except Exception as e:
log.exception("error [-] when add to forbidden")
redis_manager.add_error(url)
except Exception as e:
#del from todo , add to timeout
try:
redis_manager.del_todo(url)
log.info("success [+] delete from todo")
except Exception as e:
log.exception("error [-] when delete from todo")
redis_manager.add_error(url)
try:
redis_manager.add_timeout(url)
log.warning("success [+] add to timeout")
except Exception as e:
redis_manager.add_error(url)
log.exception("error [-] when add to timeout")
def url_last_part(self, url):
domain = parse.urlparse(url).netloc
domain_split = domain.split('.')
split_len = len(domain_split)
_url = domain_split[split_len-2] + '.' + domain_split[split_len-1]
return _url
def extension_check(self, url):
extension = url.split('.')[-1]
black_extension_list = ['pdf', 'mp4', 'mp3', 'js', 'css', 'txt', 'jpg', 'svg', 'png', 'gif',
'zip', 'bmp', 'swf', 'rar', '7z', 'mov', 'avi', 'iso', 'exe', 'pptx', 'xlsx', 'doc']
if extension not in black_extension_list:
return True
else:
return False
def crawl_more_urls(self, father_url, content):
log.info("[+] now crawing " + father_url + " for more urls")
_father_url = self.url_last_part(father_url)
#extract links & add to todo
try:
webpage = html.fromstring(content)
links = webpage.xpath('//a/@href')
for i in links:
if i[:4] == "http":
if self.extension_check(i):
_url = self.url_last_part(i)
if _url == _father_url:
log.debug(i)
redis_manager.add_todo(i)
except Exception as e:
log.exception("error [+] when crawling for more urls")
redis_manager.add_error(father_url)
def crawl(self, urls, l):
url = job_accquire(urls, l)
# log.debug("crawling "+url)
content = self.simple_request(url)
if content:
self.crawl_more_urls(url, content)
def todo_init(redis_manager: myRedis.Manage_Redis):
redis_manager.read_from_conf()
log.debug(redis_manager.status())
todo_urls = redis_manager.get_todo()
finish_urls = redis_manager.get_finish()
timeout_urls = redis_manager.get_timeout()
forbidden_urls = redis_manager.get_forbidden()
urls = todo_urls - finish_urls - timeout_urls - forbidden_urls
return urls
def job_accquire(urls, l):
l.acquire()
log.debug(urls)
url = urls.pop()
log.debug(urls)
l.release()
return url
if __name__ == "__main__":
log.warning("[+] crawling raw http contents, without rendering js")
with mp.Manager() as manager:
redis_manager = myRedis.Manage_Redis()
spider = Spider()
urls = todo_init(redis_manager)
log.debug(len(urls))
urls = manager.list(urls) # share memory
l = mp.Lock()
while True:
for i in range(SPIDER_PROCESS_NUM):
try:
p = mp.Process(target=spider.crawl, args=(urls, l))
p.start()
except:
continue
p.join()