Python写的抓取脚本[bak]


从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

从此我也订阅了该作者的百度空间,还是有很多不错的内容的:

http://hi.baidu.com/l34rn/

, ,

《 “Python写的抓取脚本[bak]” 》 有 2 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注