最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Scrapy-Redis之RedisSpider與RedisCrawlSpider詳解

 更新時(shí)間:2020年11月18日 14:12:22   作者:pengjunlee  
這篇文章主要介紹了Scrapy-Redis之RedisSpider與RedisCrawlSpider詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在上一章《Scrapy-Redis入門實(shí)戰(zhàn)》中我們利用scrapy-redis實(shí)現(xiàn)了京東圖書爬蟲的分布式部署和數(shù)據(jù)爬取。但存在以下問題:

每個(gè)爬蟲實(shí)例在啟動的時(shí)候,都必須從start_urls開始爬取,即每個(gè)爬蟲實(shí)例都會請求start_urls中的地址,屬重復(fù)請求,浪費(fèi)系統(tǒng)資源。

為了解決這一問題,Scrapy-Redis提供了RedisSpider與RedisCrawlSpider兩個(gè)爬蟲類,繼承自這兩個(gè)類的Spider在啟動的時(shí)候能夠從指定的Redis列表中去獲取start_urls;任意爬蟲實(shí)例從Redis列表中獲取某一 url 時(shí)會將其從列表中彈出,因此其他爬蟲實(shí)例將不能重復(fù)讀取該 url ;對于那些未從Redis列表獲取到初始 url 的爬蟲實(shí)例將一直處于阻塞狀態(tài),直到 start_urls列表中被插入新的起始地址或者Redis的Requests列表中出現(xiàn)待處理的請求。

在這里,我們以爬取當(dāng)當(dāng)網(wǎng)圖書信息為例對這兩個(gè)Spider的用法進(jìn)行簡單示例。

settings.py 配置如下:

# -*- coding: utf-8 -*-

BOT_NAME = 'dang_dang'

SPIDER_MODULES = ['dang_dang.spiders']
NEWSPIDER_MODULE = 'dang_dang.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False


######################################################
##############下面是Scrapy-Redis相關(guān)配置################
######################################################

# 指定Redis的主機(jī)名和端口
REDIS_HOST = 'localhost'
REDIS_PORT = 6379

# 調(diào)度器啟用Redis存儲Requests隊(duì)列
SCHEDULER = "scrapy_redis.scheduler.Scheduler"

# 確保所有的爬蟲實(shí)例使用Redis進(jìn)行重復(fù)過濾
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"

# 將Requests隊(duì)列持久化到Redis,可支持暫?;蛑貑⑴老x
SCHEDULER_PERSIST = True

# Requests的調(diào)度策略,默認(rèn)優(yōu)先級隊(duì)列
SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.PriorityQueue'

# 將爬取到的items保存到Redis 以便進(jìn)行后續(xù)處理
ITEM_PIPELINES = {
  'scrapy_redis.pipelines.RedisPipeline': 300
}

RedisSpider代碼示例

# -*- coding: utf-8 -*-
import scrapy
import re
import urllib
from copy import deepcopy
from scrapy_redis.spiders import RedisSpider


class DangdangSpider(RedisSpider):
  name = 'dangdang'
  allowed_domains = ['dangdang.com']
  redis_key = 'dangdang:book'
  pattern = re.compile(r"(http|https)://category.dangdang.com/cp(.*?).html", re.I)

  # def __init__(self, *args, **kwargs):
  #   # 動態(tài)定義可爬取的域范圍
  #   domain = kwargs.pop('domain', '')
  #   self.allowed_domains = filter(None, domain.split(','))
  #   super(DangdangSpider, self).__init__(*args, **kwargs)

  def parse(self, response): # 從首頁提取圖書分類信息
    # 提取一級分類元素
    div_list = response.xpath("http://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 = div.xpath("./div//dl[@class='inner_dl']")
      for dl in dl_list:
        item["m_cate"] = dl.xpath(".//dt/a/@title").extract_first()
        # 提取三級分類元素
        a_list = dl.xpath("./dd/a")
        for a in a_list:
          item["s_cate"] = a.xpath("./text()").extract_first()
          item["s_href"] = a.xpath("./@href").extract_first()
          if item["s_href"] is not None and self.pattern.match(item["s_href"]) is not None:
            yield scrapy.Request(item["s_href"], callback=self.parse_book_list,
                       meta={"item": deepcopy(item)})

  def parse_book_list(self, response): # 從圖書列表頁提取數(shù)據(jù)
    item = response.meta['item']
    li_list = response.xpath("http://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_desc"] = 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_first()
      item["book_publish_date"] = li.xpath("./p[@class='search_book_author']/span[2]/text()").extract_first()
      if item["book_publish_date"] is not None:
        item["book_publish_date"] = item["book_publish_date"].replace('/', '')
      item["book_press"] = li.xpath("./p[@class='search_book_author']/span[3]/a/text()").extract_first()
      yield deepcopy(item)

    # 提取下一頁地址
    next_url = response.xpath("http://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, meta={"item": item})

當(dāng)Redis 的dangdang:book鍵所對應(yīng)的start_urls列表為空時(shí),啟動DangdangSpider爬蟲會進(jìn)入到阻塞狀態(tài)等待列表中被插入數(shù)據(jù),控制臺提示內(nèi)容類似下面這樣:

2019-05-08 14:02:53 [scrapy.core.engine] INFO: Spider opened
2019-05-08 14:02:53 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2019-05-08 14:02:53 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023

此時(shí)需要向start_urls列表中插入爬蟲的初始爬取地址,向Redis列表中插入數(shù)據(jù)可使用如下命令:

lpush dangdang:book http://book.dangdang.com/

命令執(zhí)行完后稍等片刻DangdangSpider便會開始爬取數(shù)據(jù),爬取到的數(shù)據(jù)結(jié)構(gòu)如下圖所示:

RedisCrawlSpider代碼示例

# -*- coding: utf-8 -*-
import scrapy
import re
import urllib
from copy import deepcopy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy_redis.spiders import RedisCrawlSpider


class DangdangCrawler(RedisCrawlSpider):
  name = 'dangdang2'
  allowed_domains = ['dangdang.com']
  redis_key = 'dangdang:book'
  pattern = re.compile(r"(http|https)://category.dangdang.com/cp(.*?).html", re.I)

  rules = (
    Rule(LinkExtractor(allow=r'(http|https)://category.dangdang.com/cp(.*?).html'), callback='parse_book_list',
       follow=False),
  )

  def parse_book_list(self, response): # 從圖書列表頁提取數(shù)據(jù)
    item = {}
    item['book_list_page'] = response._url
    li_list = response.xpath("http://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_desc"] = 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_first()
      item["book_publish_date"] = li.xpath("./p[@class='search_book_author']/span[2]/text()").extract_first()
      if item["book_publish_date"] is not None:
        item["book_publish_date"] = item["book_publish_date"].replace('/', '')
      item["book_press"] = li.xpath("./p[@class='search_book_author']/span[3]/a/text()").extract_first()
      yield deepcopy(item)

    # 提取下一頁地址
    next_url = response.xpath("http://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)

 與DangdangSpider爬蟲類似,DangdangCrawler在獲取不到初始爬取地址時(shí)也會阻塞在等待狀態(tài),當(dāng)start_urls列表中有地址即開始爬取,爬取到的數(shù)據(jù)結(jié)構(gòu)如下圖所示:

到此這篇關(guān)于Scrapy-Redis之RedisSpider與RedisCrawlSpider詳解的文章就介紹到這了,更多相關(guān)Scrapy-Redis之RedisSpider與RedisCrawlSpider內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python3-異步進(jìn)程回調(diào)函數(shù)(callback())介紹

    Python3-異步進(jìn)程回調(diào)函數(shù)(callback())介紹

    這篇文章主要介紹了Python3-異步進(jìn)程回調(diào)函數(shù)(callback())介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Ubuntu安裝Python3.8的兩種方法詳解

    Ubuntu安裝Python3.8的兩種方法詳解

    這篇文章主要給大家介紹了關(guān)于Ubuntu安裝Python3.8的兩種方法,在Ubuntu上安裝Python非常簡單,文中介紹了兩種方法,每種方法都給出了詳細(xì)實(shí)例,需要的朋友可以參考下
    2023-09-09
  • pycocotools介紹以及在windows10下的安裝過程

    pycocotools介紹以及在windows10下的安裝過程

    這篇文章主要介紹了pycocotools介紹以及在windows10下的安裝過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 使用pandas中的DataFrame.rolling方法查看時(shí)間序列中的異常值

    使用pandas中的DataFrame.rolling方法查看時(shí)間序列中的異常值

    Pandas是Python中最受歡迎的數(shù)據(jù)分析和處理庫之一,提供了許多強(qiáng)大且靈活的數(shù)據(jù)操作工具,在Pandas中,DataFrame.rolling方法是一個(gè)強(qiáng)大的工具,在本文中,我們將深入探討DataFrame.rolling方法的各種參數(shù)和示例,以幫助您更好地理解和應(yīng)用這個(gè)功能
    2023-12-12
  • 讓你相見恨晚的十個(gè)Python騷操作

    讓你相見恨晚的十個(gè)Python騷操作

    這篇文章主要給大家介紹了十個(gè)讓你相見恨晚的Python騷操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • python中的反斜杠問題深入講解

    python中的反斜杠問題深入講解

    這篇文章主要給大家介紹了關(guān)于python中反斜杠問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python利用BeautifulSoup解析Html的方法示例

    Python利用BeautifulSoup解析Html的方法示例

    BeautifulSoup是python的一個(gè)庫,最主要的功能是從網(wǎng)頁抓取數(shù)據(jù)。下面這篇文章主要給大家介紹了關(guān)于Python利用BeautifulSoup解析Html的方法示例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • Python使用OpenPyXL處理Excel表格

    Python使用OpenPyXL處理Excel表格

    這篇文章主要介紹了Python使用OpenPyXL處理Excel表格,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 如何在sae中設(shè)置django,讓sae的工作環(huán)境跟本地python環(huán)境一致

    如何在sae中設(shè)置django,讓sae的工作環(huán)境跟本地python環(huán)境一致

    這篇文章主要介紹了如何在sae中設(shè)置django,讓sae的工作環(huán)境跟本地python環(huán)境一致,需要的朋友可以參考下
    2017-11-11
  • 使用Python開發(fā)游戲運(yùn)行腳本實(shí)現(xiàn)模擬點(diǎn)擊

    使用Python開發(fā)游戲運(yùn)行腳本實(shí)現(xiàn)模擬點(diǎn)擊

    這篇文章主要介紹了使用Python開發(fā)游戲運(yùn)行腳本實(shí)現(xiàn)模擬點(diǎn)擊,這樣我們要想實(shí)現(xiàn)手游腳本開發(fā)的第一步,就是下載Android模擬器,然后在對安卓模擬器進(jìn)行鼠標(biāo)和鍵盤的模擬,以此來實(shí)現(xiàn)自動化游戲腳本,需要的朋友可以參考下
    2021-11-11

最新評論

台南市| 南开区| 安庆市| 卓资县| 昌图县| 江西省| 绩溪县| 文成县| 湖南省| 报价| 会宁县| 鹤峰县| 中山市| 淮滨县| 青川县| 江都市| 陕西省| 东辽县| 湟源县| 黄大仙区| 镇远县| 舟山市| 龙里县| 浮梁县| 左贡县| 庄河市| 灵宝市| 灵台县| 双辽市| 五莲县| 鄂托克旗| 万源市| SHOW| 无棣县| 河间市| 顺昌县| 平昌县| 陇南市| 贵溪市| 分宜县| 鹤壁市|