python爬蟲模塊之調度模塊

調度模塊也就是對以前因此的模塊的一個調度,做爲一個流水的入口。html

下面的代碼的獲取數據部分暫時沒有寫,細節部分在實際開發中,要根據要求再定義,這裏說的是使用方法node

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")
相關文章
相關標籤/搜索