【问题标题】:Scrapy not able to scrape for the next pageScrapy 无法抓取下一页
【发布时间】:2020-12-13 09:26:54
【问题描述】:

我想抓取后续页面的信息,但是代码只允许我从第一页抓取信息。

我的代码如下:

# -*- coding: utf-8 -*-
import scrapy
from ..items import PropertyItem

class Starprop(scrapy.Spider):
name = 'starprop'
allowed_domains = ['starproperty.com']
start_urls = ['https://www.starproperty.my/to-buy/search?max_price=1000000%2B&new_launch_checkbox=on&sub_sales_checkbox=on&auction_checkbox=on&listing=For%20Sale&sort=latest&page=1']


def parse(self, response):
    item = PropertyItem ()
    property_list = response.css('.mb-4 div')

    for property in property_list:
        property_name = property.css ('.property__name::text').extract()
        property_price = property.css('.property__price::text').extract()
        property_location = property.css ('.property__location::text').extract()
        property_agent = property.css('.property__agentdetails .property__agentdetails span:nth-child(1)::text').extract()
        property_phone = property.css ('.property__agentcontacts a span::text').extract()

        item['property_name']= property_name
        item['property_price']= property_price
        item['property_location'] = property_location
        item['property_agent'] = property_agent
        item['property_phone'] = property_phone

        yield item

        next_page = response.css('.page-item:nth-child(10) .page-link::attr(href)').get()

    if next_page is not None:
        yield response.follow(next_page, callback = self.parse)

【问题讨论】:

    标签: python web-scraping scrapy scrapy-shell


    【解决方案1】:

    这就是您的allowed_domains 的全部内容(但您也需要修复缩进)。此外,我确定您想在 inside 循环中定义您的项目:

    class Starprop(scrapy.Spider):
        name = 'starprop'
        allowed_domains = ['starproperty.my']
        start_urls = ['https://www.starproperty.my/to-buy/search?max_price=1000000%2B&new_launch_checkbox=on&sub_sales_checkbox=on&auction_checkbox=on&listing=For%20Sale&sort=latest&page=1']
    
    
        def parse(self, response):
    
            property_list = response.css('.mb-4 div')
    
            for property in property_list:
                property_name = property.css ('.property__name::text').extract()
                property_price = property.css('.property__price::text').extract()
                property_location = property.css ('.property__location::text').extract()
                property_agent = property.css('.property__agentdetails .property__agentdetails span:nth-child(1)::text').extract()
                property_phone = property.css ('.property__agentcontacts a span::text').extract()
                item = PropertyItem ()
                item['property_name']= property_name
                item['property_price']= property_price
                item['property_location'] = property_location
                item['property_agent'] = property_agent
                item['property_phone'] = property_phone
    
                yield item
    
            next_page = response.css('.page-item:nth-child(10) .page-link::attr(href)').get()
    
            if next_page:
                yield response.follow(next_page, callback = self.parse)
    

    【讨论】:

      【解决方案2】:

      可能是因为缩进? 尝试改变:

          yield item
      
          next_page = response.css('.page-item:nth-child(10) .page-link::attr(href)').get()
      
      if next_page is not None:
          yield response.follow(next_page, callback = self.parse)
      

          yield item
      
          next_page = response.css('.page-item:nth-child(10) .page-link::attr(href)').get()
      
          if next_page is not None:
              yield response.follow(next_page, callback = self.parse)
      

      【讨论】:

      • 您好,我已经尝试了您的建议,但仍然无法抓取第二页。我第二页的选择器是否不正确或者我错过了任何其他代码?
      • 请把所有东西都打印出来看看next_page是什么
      猜你喜欢
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 2023-03-13
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      相关资源
      最近更新 更多