【问题标题】:Scrapy: Following pagination link to scrape data [duplicate]Scrapy:按照分页链接抓取数据[重复]
【发布时间】:2018-09-09 15:32:42
【问题描述】:

我正在尝试从页面中抓取数据并按照分页链接继续抓取。

我要抓取的页面是 --> here

# -*- coding: utf-8 -*-
import scrapy


class AlibabaSpider(scrapy.Spider):
    name = 'alibaba'
    allowed_domains = ['alibaba.com']
    start_urls = ['https://www.alibaba.com/catalog/agricultural-growing-media_cid144?page=1']

def parse(self, response):
    for products in response.xpath('//div[contains(@class, "m-gallery-product-item-wrap")]'):
        item = {
            'product_name': products.xpath('.//h2/a/@title').extract_first(),
            'price': products.xpath('.//div[@class="price"]/b/text()').extract_first('').strip(),
            'min_order': products.xpath('.//div[@class="min-order"]/b/text()').extract_first(),
            'company_name': products.xpath('.//div[@class="stitle util-ellipsis"]/a/@title').extract_first(),
            'prod_detail_link': products.xpath('.//div[@class="item-img-inner"]/a/@href').extract_first(),
            'response_rate': products.xpath('.//i[@class="ui2-icon ui2-icon-skip"]/text()').extract_first('').strip(),
            #'image_url': products.xpath('.//div[@class=""]/').extract_first(),
         }
        yield item

    #Follow the paginatin link
    next_page_url = response.xpath('//link[@rel="next"]/@href').extract_first()
    if next_page_url:
        yield scrapy.Request(url=next_page_url, callback=self.parse)

问题

  • 代码无法跟随分页链接。

你能提供什么帮助

  • 修改代码以跟随分页链接。

【问题讨论】:

    标签: python xpath web-scraping scrapy


    【解决方案1】:

    要使您的代码正常工作,您需要使用response.follow() 或类似的东西来修复损坏的链接。试试下面的方法。

    import scrapy
    
    class AlibabaSpider(scrapy.Spider):
        name = 'alibaba'
        allowed_domains = ['alibaba.com']
        start_urls = ['https://www.alibaba.com/catalog/agricultural-growing-media_cid144?page=1']
    
        def parse(self, response):
            for products in response.xpath('//div[contains(@class, "m-gallery-product-item-wrap")]'):
                item = {
                'product_name': products.xpath('.//h2/a/@title').extract_first(),
                'price': products.xpath('.//div[@class="price"]/b/text()').extract_first('').strip(),
                'min_order': products.xpath('.//div[@class="min-order"]/b/text()').extract_first(),
                'company_name': products.xpath('.//div[@class="stitle util-ellipsis"]/a/@title').extract_first(),
                'prod_detail_link': products.xpath('.//div[@class="item-img-inner"]/a/@href').extract_first(),
                'response_rate': products.xpath('.//i[@class="ui2-icon ui2-icon-skip"]/text()').extract_first('').strip(),
                #'image_url': products.xpath('.//div[@class=""]/').extract_first(),
                }
                yield item
    
            #Follow the paginatin link
            next_page_url = response.xpath('//link[@rel="next"]/@href').extract_first()
            if next_page_url:
                yield response.follow(url=next_page_url, callback=self.parse)
    

    您粘贴的代码缩进严重。我也解决了这个问题。

    【讨论】:

      【解决方案2】:

      它不起作用,因为 url 无效。如果你想继续使用scrapy.Request,你可以使用:

      next_page_url = response.xpath('//link[@rel="next"]/@href').extract_first()
      if next_page_url:
          next_page_url = response.urljoin(next_page_url)
          yield scrapy.Request(url=next_page_url, callback=self.parse)
      

      更短的解决方案:

      next_page_url = response.xpath('//link[@rel="next"]/@href').extract_first()
      if next_page_url:
          yield response.follow(next_page_url)
      

      【讨论】:

        猜你喜欢
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 2019-02-13
        • 1970-01-01
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多