zoukankan      html  css  js  c++  java
  • Python 网络爬虫(转)

    Python 网络爬虫
    2008-06-06 10:48
            转:http://proinsight.spaces.live.com/blog/cns!2056a7f1d7a3017e!144.entry
            刚刚开了一个 《计算机网络》的课,觉得很有用。正好师兄让我练习编写一个能下载网站网页的程序,正好能用上课上的知识了。为了想作一个效率不差的,而下载网页的性能瓶 颈是在网络上,所有决定用Python编写代码。刚学python没几天,学习一种语言的最好方法就是写code.下面的是我用的多线程实现的网络爬虫, 并用py2exe生成了一个exe,自身觉得py2exe不太好,又不会更好的,只能......
              这是我这些天的成果。希望有人能提出好的建议,先谢谢了!一共两个文件,一个是toolbox_insight.py,是一个工具文件另一个是test.py,是一个用到toolbox_insight.py中工具的测试文件
    #FileName: toolbox_insight.py
    from sgmllib import SGMLParser
    import threading
    import time
    import urllib2
    import StringIO
    import gzip
    import string
    import os
    #rewrite SGMLParser for start_a
    class Basegeturls(SGMLParser):   #这个Basegeturls类作用是分析下载的网页,把网页中的所有链接放在self.url中。
        def reset(self):
            self.url = []
            SGMLParser.reset(self)
        def start_a(self, attrs):
            href = [v for k, v in attrs if k == 'href']
            if href:
                self.url.extend(href)
    #for quickly finding
    class Newlist(list):#这个类其实是一个添加了find方法的LIST。当num变量在LIST中,返回True,当不在LIST中,返回False并把num按二分法插入LIST中
        def find(self, num):
            l = len(self)
            first = 0
            end = l - 1
            mid = 0
            if l == 0:
                self.insert(0,num)
                return False
            while first < end:
                mid = (first + end)/2
                if num > self[mid]:
                    first = mid + 1
                elif num < self[mid]:
                    end = mid - 1
                else:
                    break
            if first == end:
                if self[first] > num:
                    self.insert(first, num)
                    return False
                elif self[first] < num:
                    self.insert(first + 1, num)
                    return False
                else:
                    return True
            elif first > end:
                self.insert(first, num)
                return False
            else:
                return True
    #下面的reptile顾名思义是一个爬虫        
    class reptile(threading.Thread):
        #Name:       是爬虫是名字,queue是任务队列,所有的爬虫共用同一个任务队列
        #从中取出一个任务项进行运行,每个任务项是一个要下载网页的URL
        #result:     也是一个队列,将下载的网页中包含的URL放入该队列中
        #inittime:   在本程序中没有用,只是一个为了以后扩展用的
        #downloadway:是下载的网页存放的路径
        #configfile: 是配置文件,存放网页的URL和下载下后的路径
        #maxnum:     每个爬虫有个最大下载量,当下载了这么多网页后,爬虫dead
        def __init__(self, Name, queue, result, Flcok, inittime = 0.00001, downloadway = 'D:\\bbs\\',configfile = 'D:\\bbs\\conf.txt', maxnum = 10000):
            threading.Thread.__init__(self, name = Name)
            self.queue = queue
            self.result = result
            self.Flcok = Flcok
            self.inittime = inittime
            self.mainway = downloadway
            self.configfile = configfile
            self.num = 0          #已下载的网页个数
            self.maxnum = maxnum
            os.makedirs(downloadway + self.getName())      #系统调用:在存放网页的文件夹中创建一个以该爬虫name为名字的文件夹
            self.way = downloadway + self.getName() + '\\'
        def run(self):
            opener = urllib2.build_opener()     #创建一个开启器
            while True:
                url = self.queue.get()          #从队列中取一个URL
                if url == None:                 #当取得一个None后表示爬虫结束工作,用于外部方便控制爬虫的生命期
                    break
                parser = Basegeturls()          #创建一个网页分析器
                request = urllib2.Request(url) #网页请求
                request.add_header('Accept-encoding', 'gzip')#下载的方式是gzip压缩后的网页,gzip是大多数服务器支持的一种格式
                try:                                         #这样可以减轻网络压力
                    page = opener.open(request)#发送请求报文
                    if page.code == 200:       #当请求成功
                        predata = page.read() #下载gzip格式的网页
                        pdata = StringIO.StringIO(predata)#下面6行是实现解压缩
                        gzipper = gzip.GzipFile(fileobj = pdata)
                        try:
                            data = gzipper.read()
                        except(IOError):
                            print 'unused gzip'
                            data = predata#当有的服务器不支持gzip格式,那么下载的就是网页本身
                        try:
                            parser.feed(data)#分析网页
                        except:
                            print 'I am here'#有的网页分析不了,如整个网页就是一个图片
                        for item in parser.url:
                            self.result.put(item)#分析后的URL放入队列中
                        way = self.way + str(self.num) + '.html'#下面的是网页的保存,不多说了
                        
    self.num += 1
                        file = open(way, 'w')
                        file.write(data)
                        file.close()
                        self.Flcok.acquire()
                        confile = open(self.configfile, 'a')
                        confile.write( way + ' ' + url + '\n')
                        confile.close()
                        self.Flcok.release()
                    page.close()
                    if self.num >= self.maxnum:#达到最大量后退出
                        
    break
                except:
                    print 'end error'
    #和爬虫一样是个线程类,作用是将爬虫中的result中存入的URL加以处理。只要同一个服务器的网页
    class proinsight(threading.Thread):
        def __init__(self, queue, list, homepage, inqueue):
            threading.Thread.__init__(self)
            self.queue = queue#和爬虫中的result队列是同一个
            self.list = list#是上面Newlist的对象
            self.homepage = homepage#主页
            
    self.inqueue = inqueue#处理完后的URL的去处
        def run(self):
            length = len(self.homepage)
            while True:
                item = self.queue.get()
                if item == None:
                    break
                if item[0:4] == '\r\n':
                    item = item[4:]
                if item[-1] == '/':
                    item = item[:-1]
                if len(item) >= len('http://') and item[0:7] == 'http://':
                    if len(item) >= length and item[0:length] == self.homepage:
                        if self.list.find(item) == False:
                            self.inqueue.put(item)
                elif item[0:5] == '/java' or item[0:4] == 'java':
                    pass
                else:   
                    if item[0] != '/':
                        item = '/' + item
                    item = self.homepage + item
                    if self.list.find(item) == False:
                        self.inqueue.put(item)
    下面的是一个主函数过程
    我下载的网站是http://bbs.hit.edu.cn
    开始网页是http://bbs.hit.edu.cn/mainpage.php
    #FileName:test
    from toolbox_insight import *
    from Queue import Queue
    import threading
    import sys
    num = int(raw_input('Enter the number of thread:'))
    pnum = int(raw_input('Enter the number of download pages:'))
    mainpage = str(raw_input('The mainpage:'))
    startpage = str(raw_input('Start page:'))
    queue = Queue()
    key = Queue()
    inqueue = Queue()
    list = Newlist()
    thlist = []
    Flock = threading.RLock()
    for i in range(num):
        th = reptile('th' + str(i), queue, key, Flock)
        thlist.append(th)
    pro = proinsight(key, list, mainpage, inqueue)
    pro.start()
    for i in thlist:
        i.start()
    queue.put(startpage)
    for i in range(pnum):
        queue.put(inqueue.get())
    for i in range(num):
        queue.put(None)
    个人觉得用wxpython来实现用户界面和用数据库知识查找URL是更好的扩展方向

    额外补充:http://www.2cto.com/kf/201211/172371.html

  • 相关阅读:
    wpf Behavior
    wpf Trigger
    语法糖
    Lambda 表达式
    wpf 3D动画
    IEnumerable接口学习
    Delegates, Events, and Anonymous Methods 委托、事件与匿名方法
    wpf 平滑效果随记
    软件工程第一篇博客
    记考研高数第一课
  • 原文地址:https://www.cnblogs.com/wickedboy237/p/3040321.html
Copyright © 2011-2022 走看看