-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspider.xml
304 lines (272 loc) · 7.99 KB
/
spider.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN"
"http://www.oasis-open.org/docbook/xml/5.0b5/dtd/docbook.dtd" [
<!ENTITY article.author.xml SYSTEM "../common/article.author.xml">
<!ENTITY book.info.legalnotice.xml SYSTEM "../common/book.info.legalnotice.xml">
<!ENTITY book.info.abstract.xml SYSTEM "../common/book.info.abstract.xml">
]>
<article xml:base="http://netkiller.sourceforge.net/article/"
xmlns="http://docbook.org/ns/docbook" xml:lang="zh-cn">
<articleinfo>
<title>Python Spider 爬虫实例</title>
&article.author.xml;
&book.info.legalnotice.xml;
<abstract>
<para>.</para>
</abstract>
&book.info.abstract.xml;
<keywordset>
<keyword>spider</keyword>
<keyword></keyword>
<keyword></keyword>
</keywordset>
</articleinfo>
<section>
<title>爬虫实力</title>
<para>主要的功能是爬出所有URL</para>
<para>稍加修改可以加入SQL注入检查,跨站脚本攻击检查等等...</para>
<screen>
<![CDATA[
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
##############################################
# Home : http://netkiller.github.com
# Author: Neo <[email protected]>
##############################################
##############################################
from multiprocessing import Process
from multiprocessing import Pool
from html.parser import HTMLParser,HTMLParseError
import asyncore, asynchat, socket, threading, queue
import subprocess, os, sys, getopt, configparser, logging
import string, re
import random,time
import urllib.request, urllib.parse, http.client
#from urllib.parse import urlparse
queue = queue.Queue()
class MyHTMLParser(HTMLParser):
urls = []
def handle_starttag(self, tag, attrs):
# Only parse the 'anchor' tag.
if tag == "a":
# Check the list of defined attributes.
for name, value in attrs:
# If href is defined, print it.
if name == "href":
#print name, "=", value
if value and (value.find('javascript') == -1) and (value not in ('#')):
self.urls.append(value)
#def handle_endtag(self, tag):
#print("Encountered an end tag:", tag)
# pass
#def handle_data(self, data):
#print("Encountered some data:", data)
# pass
def gethref(self):
return self.urls
class Spider():
logfile = '/tmp/spider.log'
isdebug = False
depths = 0
link = []
unlink = []
referer = ''
useragent = 'Neo spider'
domain = r''
baseurl = r''
skip = []
ignore = []
threadname = ''
def __init__(self, threadname = None):
logging.basicConfig(level=logging.NOTSET,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=self.logfile,
filemode='a')
self.logging = logging.getLogger()
if threadname:
self.threadname = '|'+threadname
def setDebug(self,isdebug):
self.isdebug = isdebug
self.logging.debug('Enable Debug')
def setDomain(self, tmp):
if tmp:
self.domain = tmp
def setReferer(self,tmp):
if tmp:
self.referer = tmp
def setUseragent(self,tmp):
if tmp:
self.useragent = tmp
def setBaseUrl(self,tmp):
if tmp:
self.baseurl = tmp
def ufilter(self,url):
if (url not in self.link) :
self.link.append(url)
else:
if url not in self.skip:
self.skip.append(url)
self.logging.warning('Skip ' + url)
return(None)
if url[0:1] == '/':
return(self.baseurl + url)
elif url.find('http://') == -1:
return(self.baseurl +'/'+ url)
else:
if url.find(self.domain) == -1:
if url not in self.ignore:
self.logging.warning('Ignore ' + url)
self.ignore.append(url)
return(None)
return(url)
def working(self, myurl):
self.depths = self.depths + 1
if self.depths > 256:
return()
#if self.isdebug:
# self.logging.debug('>>>' + str(self.depths))
url = self.ufilter(myurl)
if url == None:
return()
else:
self.link.append(myurl)
if self.baseurl:
urlparse = urllib.parse.urlparse(url)
self.setBaseUrl(urlparse.scheme+ '://' + urlparse.netloc)
#self.setDomain(urlparse.netloc)
#print(urlparse)
#uri.path
#uri.params
#uri.query
#uri.fragment
try:
lines = []
parser = MyHTMLParser()
#html = urllib.request.urlopen(myurl)
#body = html.read()
#html.close()
req = urllib.request.Request(url)
req.add_header('User-agent', self.useragent)
req.add_header('Referer', self.referer)
response = urllib.request.urlopen(req, timeout = 10)
status = response.status
reason = response.reason
headers = response.info()
log = str(status)+' '+reason+' '+myurl+ ' ('+ str(self.depths)+self.threadname+') '
if self.isdebug:
#print(response.geturl())
print(log)
#print(headers)
if headers['Content-Type'] in ('text/html'):
if status == 200:
body = response.read()
parser.feed(bytes.decode(body))
lines = parser.gethref()
self.logging.info(log)
elif status == 302:
self.unlink.append(myurl)
self.logging.critical(log)
else:
self.logging.warning(log)
response.close()
if lines:
self.referer = random.choice(lines)
for line in lines:
#result = re.match ('/http://"(.*)"/"(.*)"', line)
self.working(line)
self.depths = self.depths - 1
#if self.isdebug:
# self.logging.debug('<<<' + str(self.depths))
else:
self.logging.warning(log + ' ' + headers['Content-Type'])
response.close()
except socket.timeout as e:
self.logging.error(str(e) +' '+ myurl)
except urllib.error.URLError as e:
if self.isdebug:
print (str(e) +' '+myurl + ' - ' + url)
if (e.code == 404):
self.unlink.append(myurl)
else:
print(e.code)
self.logging.critical(str(e) +' '+ myurl)
except urllib.error.HTTPError as e:
self.logging.critical(str(e) +' '+ myurl)
except HTMLParseError as e:
self.logging.error(str(e) +' '+ myurl)
if self.isdebug:
print (str(e) +' '+ myurl)
except UnicodeDecodeError as e:
self.logging.critical(str(e) +' '+ myurl)
except ValueError as e:
if self.isdebug:
print (str(e) +' '+ myurl)
#else:
#self.logging.error(myurl)
#finally:
#self.depths = self.depths - 1
#pass
class ThreadSpider(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
self.spider = Spider(str(self.name))
self.spider.setDebug(True)
def run(self):
while True:
#grabs host from queue
host = self.queue.get()
self.spider.setDomain(host)
self.spider.working(host)
#signals to queue job is done
self.queue.task_done()
def Multithreading():
workers = 5
hosts = ['http://www.example.com/','http://brand.example.com/','http://list.example.com/','http://item.example.com/']
#spawn a pool of threads, and pass them queue instance
for i in range(workers):
t = ThreadSpider(queue)
t.setDaemon(True)
t.start()
#populate queue with data
for host in hosts:
queue.put(host)
#wait on the queue until everything has been processed
queue.join()
#p = Pool(processes = 5)
#p.map(test, [b'http://www.163.com/',b'http://www.sina.com/',b'http://www.qq.com/'])
def test(url):
spider = Spider()
spider.setDebug(True)
spider.working(url)
print(url)
def daemon(isdaemon):
if isdaemon :
pid = os.fork()
if pid > 0:
sys.exit(0)
def main():
daemon(isdaemon = False)
myurl = r'http://www.example.com'
#myurl = r'http://www.example.com/mobile.html'
try:
start = time.time()
spider = Spider()
spider.setDebug(True)
spider.setDomain('www.example.com')
spider.setBaseUrl(myurl)
spider.working(myurl)
print("Elapsed Time: %s" % (time.time() - start))
except RuntimeError as e:
print(e)
if __name__ == '__main__':
try:
main()
#Multithreading()
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")
]]>
</screen>
</section>
</article>