天天看點

scrapy_redis分布式爬蟲scrapy_redis分布式爬蟲

scrapy_redis分布式爬蟲

首先安裝redis 然後redis連接配接

scrapy_redis分布式爬蟲scrapy_redis分布式爬蟲
scrapy_redis分布式爬蟲scrapy_redis分布式爬蟲

scrapy_redis 相對于scrapy效率更高,速度更快,代碼差別在于

scrapy_redis分布式爬蟲scrapy_redis分布式爬蟲
scrapy_redis分布式爬蟲scrapy_redis分布式爬蟲

1,繼承的父類不一樣(可以自己檢視源碼)

2,增加redis_key

3,在settings.py檔案裡增加上圖四句功能代碼

DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
SCHEDULER_PERSIST = True
REDIS_URL = "redis://127.0.0.1:6379"
           

以爬取蘇甯圖書為例 代碼如下

class DangdangSpider(RedisSpider):    		     
	 name = 'dangdang'
     allowed_domains = ['dangdang.com']
     redis_key = "dangdang"
     # start_urls = ['http://book.dangdang.com/']

    def parse(self, response):
        # 大分類分組
        div_list = response.xpath("//div[@class='con flq_body']/div")
        for div in div_list:
            item = {}
            item["b_cate"] = div.xpath("./dl/dt//text()").extract()
            item["b_cate"] = [i.strip() for i in item["b_cate"] if len(i.strip()) > 0]
            # 擷取中間分類
            dl_list = response.xpath("./div//dl[@class='inner_dl']")
            for dl in dl_list:
                item["m_cate"] = dl.xpath("./dt//text()").extract()
                item["m_cate"] = [i.strip() for i in item["m_cate"] if len(i.strip()) > 0][0]
                # 擷取小分類
                a_list = dl.xpath("./dd/a")
                for a in a_list:
                    item["s_href"] = a.xpath("./@href").extract_first()
                    item["s_cate"] = a.xpath("./@title").extract_first()
                    if item["s_href"] is not None:
                        yield scrapy.Request(item["href"], callback=self.parse_book_list, dont_filter=True, meta={"item": deepcopy(item)})

    def parse_book_list(self, response):
        item = response.meta["item"]
        li_list = response.xpath(".//ul[@class='bigimg']/li")
        for li in li_list:
            item["book_img"] = li.xpath("/a[@class='pic']/img/@src").extract_first()
            if item["book_img"] == "images/model/guan/url_none.png":
                item["book_img"] = li.xpath("/a[@class='pic']/img/@data-original").extract_first()
            item["book_name"] = li.xpath("./p[@class='name']/a/@title").extract_first()
            item["book_info"] = li.xpath("./p[@class='detail']/text()").extract_first()
            item["book_price"] = li.xpath(".//span[@class='search_now_price']/text()").extract_first()
            item["book_author"] = li.xpath("./p[@class='search_book_author']/span[1]/a/text()").extract()
            item["book_publish_data"] = li.xpath("./p[@class='search_book_author']/span[2]/text()").extact_first()
            item["book_press"] = li.xpath("./p[@class='search_book_author']/span[3]/a/text()").extract_first()
            print(item)
            # 下一頁
            next_url = response.xpath(".//li[@class='next']/a/@href").extract_first()
            if next_url is not None:
                next_url = urllib.parse.urljoin(response.url, next_url)
                yield scrapy.Request(next_url, callback=self.parse_book_list, dont_filter=True,  meta={"item": item})
           

程式運作和scrapy相同scrapy crawl dangdang

運作之後會出現

scrapy_redis分布式爬蟲scrapy_redis分布式爬蟲

需要去redis裡設定

scrapy_redis分布式爬蟲scrapy_redis分布式爬蟲

即完成操作。

如果出現此種情況:

scrapy_redis分布式爬蟲scrapy_redis分布式爬蟲

關于user-agent 以及robot協定及ip問題不再叙述

此種情況應是window的redis資料庫,而且lpush時,使用的是redis。windos那個檔案,這樣插入的redis_key,在你爬蟲伺服器上是查不到這個值的。

使用 redis-cli -h ip -p 6379 這樣連結資料,再lpush操作。