python爬虫模块之调度模块

调度模块也就是对之前所以的模块的一个调度,作为一个流水的入口。

下面的代码的获取数据部分暂时没有写,细节部分在实际开发中,要根据要求再定义,这里说的是使用方法

from savedb import DataOutput
from getnodelist import GetNodeList
from gethtml import Gethtml
from urlqueue import URLQueue


class Run(object):
    def __init__(self):
        self.queue = URLQueue()
        self.downloader = Gethtml()
        self.parser = GetNodeList()
        self.output = DataOutput()

    def crawl(self, root_url):
        # 添加入口URL
        self.queue.add_new_url(root_url)
        # 判断URL管理器是否有新的URL,同时计算抓取了多少个url
        while (self.queue.has_new_url() and self.queue.old_url_size() < 100):
            try:
                new_url = self.queue.get_new_url()
                html = self.downloader.get_source(new_url)
                new_urls = self.parser.use_xpath(new_url, html)
                self.queue.add_new_urls(new_urls)
                # 数据存储器存储文件
                data="" #datalist一般是上面取xpath获取值的一个集合这里略。
                self.output.store_data(data)
                print("已经抓取%s个链接" % self.queue.old_url_size())
            except Exception:
                print("err")

if __name__ == "__main__":
    spider_man = Run()
    spider_man.crawl("https://www.baidu.com")

  

猜你喜欢

转载自www.cnblogs.com/c-x-a/p/9175327.html
今日推荐