从Freebuf上找到的一个代码写得非常清晰的抓取脚本,效果也是杠杠的,放在这儿做个备份,而且时不时的需要用一用(PS:我记得当时改写了一下,可以抓取其他板块的文章,但是不知道放哪去了,从此我深知备份的重要性!),并且可以从这个脚本中通晓此类的问题的简单处理方法。
#!/usr/bin/env python
#coding=utf-8
'''
Freebuf工具抓取脚本
'''
import re,sys
from urllib import urlopen
from Queue import Queue
from threading import Thread
from time import strftime
baseUrl='http://www.freebuf.com/tools/page/'
outPut='FreebufToolsListX.html'
pageNum=32+1
threadNum=10
urlList=[]
threadList=[]
urlNum=0
def spiderIndex(url):
'''
spider function
'''
global urlNum
try:
res=urlopen(url)
except Exception,e:
print '[-] [%s] [Error] [%s]'
if res.getcode()==200:
html=res.read()
lines=html.split('n')
for line in lines:
rex=re.search(r'(<dt><a href=")(http://www.freebuf.com/tools/d*.html)(" target="_blank">).*',line)
if rex!=None:
urlNum+=1
urlList.append(rex.group())
sys.stdout.write('r[*] [%s] [Working] [%s]'%(str(strftime('%X')) ,str(urlNum)))
class WorkThread(Thread):
'''
work thread
'''
def __init__(self,q):
Thread.__init__(self)
self.q=q
def run(self):
while True:
if self.q.empty()==True:
break
_url=baseUrl+str(self.q.get())
spiderIndex(_url)
def main():
'''
main function
'''
q=Queue(maxsize=0)
for i in xrange(1,pageNum,1):
q.put(i)
print '[+] [%s] [Start]'%strftime('%X')
spiderIndex('http://www.freebuf.com/tools')
for i in xrange(threadNum):
t=WorkThread(q)
threadList.append(t)
for i in threadList:
i.start()
for i in threadList:
i.join()
f=open(outPut,'ab')
f.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />n')
f.write('<title>Freebuf Tools List</title>n')
f.write('<center><h1><b>Freebuf Tools List</b></h1>n'+'Time:'+str(strftime("%Y-%b-%d %X"))+' Count:'+str(len(urlList))+'</center><hr/>n<h5>n')
for line in urlList:
f.write(line+'</br>n')
f.close()
print 'n[+] [%s] [End] [All Done!]'%strftime('%X')
print '[+] [%s] [Save As] [%s]'%(strftime('%X'),outPut)
if __name__=='__main__':
main()
原文地址:http://www.freebuf.com/tools/25746.html
从此我也订阅了该作者的百度空间,还是有很多不错的内容的:
《 “Python写的抓取脚本[bak]” 》 有 2 条评论
真假百度蜘蛛的甄别
http://huoding.com/2017/01/19/592
http://baidu.com/search/spider.htm
`
当有 User-Agent 是 Baiduspider 的请求时,我们可以通过 host 命令反解 ip 来判断,Baiduspider 的 hostname 以 *.baidu.com 或 *.baidu.jp 的格式命名,其它的则可以视为非法的蜘蛛。
`
用shell脚本反查搜索引擎蜘蛛IP
http://www.52os.net/articles/shell-get-search-engine-spiders-ips.html