C#开源爬虫NCrawler源代码解读以及将其移植到python3.2(4)

在上一节我们回顾了python 多线程的知识。

queue这个线程安全的序列正是python用来实现线程池的关键。我们可以把爬虫需要解析的URL放入这个序列中,供其它空闲的线程获取并使用。

线程池的实现:

import ThreadPool.dlthread

class threadpool:
    def __init__( self,queue,handlers,maxdepth,num_of_threads):
        self.queue = queue
        self.handlers=handlers
        self.maxdepth=maxdepth
        self.threads = []
        self.__createThreadPool(num_of_threads)

    def __createThreadPool( self, num_of_threads):
        for i in range(num_of_threads):
            thread = ThreadPool.dlthread.dlthread(self.queue,self.handlers,self.maxdepth)
            self.threads.append(thread)

    def wait_for_complete(self):
        while len(self.threads):
            thread = self.threads.pop()
            thread.setDaemon(True)
            thread.start()
            if thread.isAlive():
                thread.join()

可以看到python 的线程池还挺简单的。下面的代码是工作线程的实现:

import threading
import Crawler.propertybag
import copy

class dlthread(threading.Thread):
    def __init__(self, queue,handlers,maxdepth):
        super(dlthread,self).__init__()
        self.maxdepth=maxdepth
        self.queue=queue
        self.handlers=copy.deepcopy(handlers)#深表复制
        self.handlers_num=len(handlers)

    def run(self):
        while True:
            urltuple=self.queue.get()
            if self.handlers_num > 0:
                ps=Crawler.propertybag.propertybag(urltuple[1],self.queue,self.maxdepth)
                for i in range(0,self.handlers_num):
                    self.handlers[i].Handle(urltuple[0],ps)
            self.queue.task_done()

其中propertybag是用来在管道(Handler)间传递数据用的。为了线程间不互相干扰,所有的Handler都是进行深表复制后才赋予线程操作的。 所有的管道(在NCrawler中叫pipeline,我这里叫Handler)实现的基类是:

class BaseHandler:
    def __init__(self):
        return

    def Handle(self,url,pbags):
        pass

最后是Crawler类:

import queue
import time
from ThreadPool.threadpool import threadpool
import Handler.HTMLHandler
from Crawler.cyclethread import cyclethread

class Crawler:
    def __init__(self,starturl, handles, threads_num=5, maxdepth=14):
        if handles is None or len(handles) == 0:
            raise Exception('No handlers is given')
        self.queue=queue.Queue()
        self.starturl=starturl
        self.queue.put((starturl,0))
        self.maxdepth=maxdepth
        if type(handles) is list and len(handles)>0:
            self.handlers=handles
        else:
            raise Exception('no handlers is given')
        self.threads_num=threads_num


    def crawl(self):
        tp=threadpool(maxdepth=self.maxdepth,queue=self.queue,handlers=self.handlers,num_of_threads=self.threads_num)
        tp.wait_for_complete()

    def cyclecrawl(self,period):
        ct=cyclethread(self.queue,(self.starturl,0),period)
        ct.setDaemon(True)
        ct.start()
        self.crawl()

它负责开启线程池并按照用户指定的方式运行爬虫。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱知菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值