Python 2.7 如果两个页面具有相同的链接,如何在刮取过程中移动到下一页?

Python 2.7 如果两个页面具有相同的链接,如何在刮取过程中移动到下一页?,python-2.7,scrapy,scrapy-spider,Python 2.7,Scrapy,Scrapy Spider,我正在使用scrapy刮网站()。我已经使用for循环和yield函数从这个页面中获取了所有数据 def parse(self, response): self.main_cat=response.xpath('//div[@id="products_content"]/div/text()').extract() self.sub_cat=response.xpath('//div[@class="accordion"]/div[@class="title"]/text()')

我正在使用scrapy刮网站()。我已经使用for循环和yield函数从这个页面中获取了所有数据

def parse(self, response):
    self.main_cat=response.xpath('//div[@id="products_content"]/div/text()').extract()
    self.sub_cat=response.xpath('//div[@class="accordion"]/div[@class="title"]/text()').extract()
    Onclick=response.xpath('//div[@class="accordion"]/div[@class="no_title subtitle_chck"]/@onclick').extract()
    for index in range(len(Onclick)):
        sub_sub_cat=response.xpath('//div[@class="accordion"]/div[@class="no_title subtitle_chck"]/label/text()').extract_first()
        removeSearchWord=Onclick[index].replace("submitSearch(","")
        numericData=removeSearchWord.replace(");","").split(',')
        absolute_url="https://portal.orio.com/webapp/wcs/stores/servlet/SearchDisplayView?storeId=11901&catalogId=10051&langId=-150&pageView=detailed&beginIndex=0&sType=SimpleSearch&categoryId="+numericData[0]+"&showResultsPage=true&navCat="+numericData[1]+"_"+numericData[2]+"&urlLangId=-150&removeFiltersOg=ALL&sortField=name&orderBy=7"
        yield Request(absolute_url, callback=self.page)

def page(self,response):
    product_page_url=response.xpath('//td[@class="information"]/a/@href').extract()
    for url in product_page_url:
        yield Request(url, callback=self.product)

在最后一个yield函数之后,哪一行代码让我继续抓取所有其他页面。我知道需要一些ajax调用,但我不知道如何实现它们。您是否愿意添加这行代码,因为我尝试了很多方法来找到解决方案,我的最后一个问题也是关于这一点的,答案很好,但我没有得到答案

实际上,下一个页面url就在那里。 它是

您可以使用xpath选择器和一些正则表达式来提取:

url = response.xpath('//a[contains(img/@src,"paging_next")]/@onclick').re("setPage\('(.+?)'")[0]
Out[1]: 'https://portal.orio.com/webapp/wcs/stores/servlet/AjaxCatalogSearchResultView?pageView=detailed&searchTermScope=&orderBy=7&categoryId=146003&beginIndex=25&pageSize=25&maxPrice=&searchType=1002&sortField=name&resultCatEntryType=&searchTerm=&sType=SimpleSearch&filterTerm=&manufacturer=&catalogId=10051&langId=-150&showResultsPage=true&storeId=11901&metaData=YnV5YWJsZToxPE1UQFNQPi1zdXBlcnNlc3Npb246KDEgMyA3KSBBTkQgcHJpY2VfU0VLXzIxOlsqIFRPICpdIEFORCAtcHJpY2VfU0VLXzIxOlsqIFRPIDBdPE1UQFNQPnB1Ymxpc2hlZDox&minPrice='
这是一个丑陋的url,但它在scrapy中工作得很好:)

一般分页逻辑如下所示:

def parse(self, response):
    product_urls = ...
    for url in product_urls:
        yield Request(url, self.parse_product)
    # next page
    next_page = ...  
    if next_page:
        yield Request(next_page, self.parse)
    else:
        self.log('oh no, last page was: {}'.format(response.url), level=logging.INFO)

谢谢这种获取url的方式只会导致第二页。第三页呢?一旦你们抓取了第二页,你们就可以在里面找到第三页的url。请参阅我的编辑以了解scrapy中常见的分页循环。您可以看到共有125种产品,通过这种方式可以生成50种产品(两页)的数据。@DanyalMughal我不确定是否理解您的意思,但您必须自己解决其余问题。我解释了如何获得下一页,以及如何在scrapy中开发分页循环-这个示例确实有效,您只需要自己将所有部分组合在一起即可。谢谢。谢谢你的帮助。请你回答我的上一个问题好吗?这个问题已经回答了,但我不明白