Python 刮皮不';f跟随下一页url,为什么?

Python 刮皮不';f跟随下一页url,为什么?,python,scrapy,Python,Scrapy,我正在用Scrapy 1.4.0清理这个网站。当我运行spider时,一切都很顺利,直到它进入“下一页”部分。代码如下: # -*- coding: utf-8 -*- import scrapy #import time class OlxarSpider(scrapy.Spider): name = "olxar" allowed_domains = ["olx.com.ar"] start_urls = ['https://www.olx.com.ar/celulares-telef

我正在用Scrapy 1.4.0清理这个网站。当我运行spider时,一切都很顺利,直到它进入“下一页”部分。代码如下:

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

class OlxarSpider(scrapy.Spider):
name = "olxar"
allowed_domains = ["olx.com.ar"]
start_urls = ['https://www.olx.com.ar/celulares-telefonos-cat-831']

def parse(self, response):
    #time.sleep(10)
    response = response.replace(body=response.body.replace('<br>', '')) 
    SET_SELECTOR = '.item'
    for item in response.css(SET_SELECTOR):
        PRODUCTO_SELECTOR = '.items-info h3 ::text'
        yield {
            'producto': item.css(PRODUCTO_SELECTOR).extract_first().replace(',',' '),
            }

    NEXT_PAGE_SELECTOR = '.items-paginations-buttons a::attr(href)'
    next_page = response.css(NEXT_PAGE_SELECTOR).extract_first().replace('//','https://')
    if next_page:
        yield scrapy.Request(response.urljoin(next_page),
            callback=self.parse
            )   
#-*-编码:utf-8-*-
进口羊瘙痒
#导入时间
OlxarSpider类(羊瘙痒蜘蛛):
name=“olxar”
允许的_域=[“olx.com.ar”]
起始URL=['https://www.olx.com.ar/celulares-telefonos-cat-831']
def解析(自我,响应):
#时间。睡眠(10)
response=response.replace(body=response.body.replace(“
”,“)) 设置_选择器='。项' 对于response.css中的项(设置选择器): PRODUCTO_选择器='。项目信息h3::文本' 屈服{ “producto”:item.css(producto\u选择器).extract\u first().replace(',',''), } 下一页\选择器='。项目分页按钮a::attr(href)' next_page=response.css(next_page_SELECTOR).extract_first().replace('/','https://')) 如果下一页: 生成scrapy.Request(response.urljoin)(下一页), callback=self.parse )
我在其他问题中看到,一些人在
请求中添加了
dont\u filter=True
属性,但这对我不起作用。它只是让蜘蛛在前两页上循环。 我添加了
replace('/','https://')
部分来修复原始href,该href没有
https:
,后面不能跟Scrapy。 另外,当我运行spider时,它会删除第一页,然后返回
[scrapy.dupefilters]DEBUG:Filtered duplicate request:-不再显示重复项(请参阅DUPEFILTER\u DEBUG以显示所有重复项)
为什么它过滤第二个页面就像是重复的,而显然不是


我在评论中应用了Tarun Lalwani解决方案。我太错过那个细节了!修正后效果很好,谢谢

您的问题是css选择器。在第1页上,它与下一页链接相匹配。在第2页上,它与上一页和下一页链接相匹配。从中,您可以使用
extract_first()
选择第一页,这样您就可以只在第一页和第二页之间旋转

解决方案很简单,您需要更改css选择器

NEXT_PAGE_SELECTOR = '.items-paginations-buttons a::attr(href)'


这将只识别下一页url,只识别其奇怪的代码。如果使用response.urljoin,为什么需要.replace('/','https://')?请提供spider的所有代码。我已经尝试过不使用https部分,并且得到了相同的结果,所以没有问题。我的网页或代码中缺少一些东西。
NEXT_PAGE_SELECTOR = '.items-paginations-buttons a.next::attr(href)'