【问题标题】:Scrapy - scraping overview-page and detail-page?Scrapy - 抓取概览页面和详细信息页面?
【发布时间】:2021-11-05 21:38:30
【问题描述】:

我尝试使用 scrapy 抓取以下网站 -

当我只从概览页面中抓取信息时,它工作得很好 (如名称、价格、链接) 它返回 1535 行。

import scrapy

class WhiskeySpider(scrapy.Spider):
  name = "whisky"
  allowed_domains = ["whiskyshop.com"]
  start_urls = ["https://www.whiskyshop.com/scotch-whisky"]

  def parse(self, response):
    for products in response.css("div.product-item-info"):
      tmpPrice = products.css("span.price::text").get()
      if tmpPrice == None:
        tmpPrice = "Sold Out"
      else:
        tmpPrice = tmpPrice.replace("\u00a3",""),
      yield {
        "name": products.css("a.product-item-link::text").get(),
        "price": tmpPrice,
        "link": products.css("a.product-item-link").attrib["href"],
      }
    
    nextPage = response.css("a.action.next").attrib["href"]
    if nextPage != None:
      nextPage = response.urljoin(nextPage)
      yield response.follow(nextPage, callback=self.parse)

现在我还想为每个项目抓取一些额外的详细信息 (如升、百分比、面积),我想要一行包含 3 个主要信息和 3 个详细信息

我使用以下代码进行了尝试 - 但效果不佳:

import scrapy

class WhiskeySpider(scrapy.Spider):
  name = "whiskyDetail"
  allowed_domains = ["whiskyshop.com"]
  start_urls = ["https://www.whiskyshop.com/scotch-whisky"]

  def parse(self, response):
    for products in response.css("div.product-item-info"):
      tmpPrice = products.css("span.price::text").get()      
      tmpLink = products.css("a.product-item-link").attrib["href"]
      tmpLink = response.urljoin(tmpLink)
      
      if tmpPrice == None:
        tmpPrice = "Sold Out"
      else:
        tmpPrice = tmpPrice.replace("\u00a3",""),
      yield {
        "name": products.css("a.product-item-link::text").get(),
        "price": tmpPrice,
        "link": tmpLink,
      }

      yield scrapy.Request(url=tmpLink, callback=self.parseDetails)                    
    
    nextPage = response.css("a.action.next").attrib["href"]
    if nextPage != None:
      nextPage = response.urljoin(nextPage)
      yield response.follow(nextPage, callback=self.parse)
  
  def parseDetails(self, response):
    tmpDetails = response.css("p.product-info-size-abv span::text").getall()
    yield {
      "litre": tmpDetails[0],
      "percent": tmpDetails[1],
      "area": tmpDetails[2]
    }

代码似乎在无限循环中运行 在日志中我看到他有时会以 429 未知状态重试

2021-11-05 22:24:14 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.whiskyshop.com/benrinnes-10-year-old-that-boutique-y-whisky-company>
{'litre': '50cl', 'percent': '56.8% abv', 'area': 'Speyside'}
2021-11-05 22:24:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.whiskyshop.com/bruichladdich-28-year-old-batch-19-that-boutique-y-whisky-company> (referer: https://www.whiskyshop.com/scotch-whisky)
2021-11-05 22:24:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.whiskyshop.com/bruichladdich-28-year-old-batch-19-that-boutique-y-whisky-company>
{'litre': '50cl', 'percent': '48.5%% abv', 'area': 'Islay'}
2021-11-05 22:24:17 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET https://www.whiskyshop.com/westport-21-year-old-batch-1-that-boutique-y-whisky-company> (failed 1 times): 429 Unknown Status
2021-11-05 22:24:18 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.whiskyshop.com/strathmill-22-year-old-batch-7-that-boutique-y-whisky-company> (referer: https://www.whiskyshop.com/scotch-whisky)
2021-11-05 22:24:18 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.whiskyshop.com/strathmill-22-year-old-batch-7-that-boutique-y-whisky-company>
{'litre': '50cl', 'percent': '49.6% abv', 'area': 'Speyside'}
2021-11-05 22:24:20 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET https://www.whiskyshop.com/benromach-40-year-old> (failed 1 times): 429 Unknown Status
2021-11-05 22:24:22 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET https://www.whiskyshop.com/monkey-shoulder-fever-tree-gift-pack> (failed 1 times): 429 Unknown Status
2021-11-05 22:24:25 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET https://www.whiskyshop.com/catalog/product/view/id/21965/s/nc-nean-organic-single-malt/category/246/> (failed 1 times): 429 Unknown Status

在 json-output 中,两个信息都不在一行中(主要信息和详细信息):

{"name": "Port Charlotte Islay Barley 2013 ", "price": ["65.00"], "link": "https://www.whiskyshop.com/port-charlotte-islay-barley-2013"},
{"name": "Bruichladdich Bere Barley 2011 ", "price": ["70.00"], "link": "https://www.whiskyshop.com/bruichladdich-bere-barley-2011"},
{"name": "Glen Grant 1950 68 Year Old ", "price": ["4,999.99"], "link": "https://www.whiskyshop.com/glen-grant-1950-68-year-old"},
{"name": "Linkwood 1981 Private Collection ", "price": ["1,250.00"], "link": "https://www.whiskyshop.com/linkwood-1981-private-collection"},
{"name": "Linkwood 1980 40 Year Old Private Collection ", "price": ["999.99"], "link": "https://www.whiskyshop.com/linkwood-1980-40-year-old-private-collection"},
{"name": "Dimensions Linkwood 2009 12 Year Old", "price": ["89.99"], "link": "https://www.whiskyshop.com/dimensions-linkwood-2009-12-year-old"},
{"name": "Dimensions Highland Park 2007 13 Year Old", "price": ["114.00"], "link": "https://www.whiskyshop.com/dimensions-highland-park-2007-13-year-old"},
{"litre": "70cl", "percent": "54.9% abv", "area": "Highland"},
{"litre": "70cl", "percent": "54.7% abv", "area": "Islay"},
{"litre": "70cl", "percent": "46% abv", "area": "Highland"},
{"litre": "70cl", "percent": "52.1% abv", "area": "Islay"},
{"litre": "70cl", "percent": "43% abv", "area": "Speyside"},
{"litre": "70cl", "percent": "43% abv", "area": "Highland"},

我做错了什么,如何在一行中获取主要和详细信息? (并且没有重试错误)

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    你必须下载延迟,否则被429状态码阻塞/重试/连接丢失等。 我的 settings.py 文件:

    USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
    ROBOTSTXT_OBEY = False
    DOWNLOAD_DELAY = 4
    

    在概览页面和详细信息页面的情况下获取数据的替代和最简单的解决方案是使用CrawlSpider。我已经在 start_urls 中进行了分页,您可以根据需要增加或减少页码范围。这里每页​​包含 100 项。

    代码:

    from scrapy.spiders import CrawlSpider, Rule
    from scrapy.linkextractors import LinkExtractor
    
    
    class ShopSpider(CrawlSpider):
        name = 'shop'
        start_urls = ['https://www.whiskyshop.com/scotch-whisky?p='+str(x)+''for x in range(1,5)]
    
        rules = (
            Rule(LinkExtractor(restrict_xpaths='//a[@class="product-item-link"]'),callback='parse', follow=False),)
    
        def parse(self, response):
            yield {
                'Name': response.xpath('//h1[@class="page-title"]/text()').get().strip(),
                'Price':response.xpath('(//span[@class="price"])[1]/text()').get(),
                'Litre':response.xpath('(//*[@class="product-info-size-abv"]/span)[1]/text()').get(),
                'Percent':response.xpath('(//*[@class="product-info-size-abv"]/span)[2]/text()').get(),
                'Area':response.xpath('(//*[@class="product-info-size-abv"]/span)[3]/text()').get(),
                'LINK': response.url}
    

    【讨论】:

    • 非常感谢 - 看起来很棒。但并不是代码被硬编码为只抓取网站的前两页。有没有办法刮掉所有可用的页面。就像我用上面的下一页逻辑做的那样?
    • 首先,看看url中的总页数是多少,然后把它放在范围内。从 start_urls 开始。我将页面范围(1,2)增加到(1,5),输出为400个项目。'item_scraped_count':400,。这是准确的分页方式。
    • @Rapid1898,我已经更改了页码并查看了变化,它工作正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多