我正在尝试对此站点的页面(http://www.geny-interim.com/offres/)进行分页。问题是我使用css选择器通过下面的代码遍历每个页面
next_page_url=response.css('a.page:nth-child(4)::attr(href)').extract_first()
if next_page_url:
yield scrapy.Request(next_page_url)但这样做只会分页到两个页面,然后css选择器不会像预期的那样工作。我也试着使用这个:
response.xpath('//*[contains(text(), "›")]/@href/text()').extract_first()但这也产生了值错误。任何帮助都会得到支持。
发布于 2019-04-28 21:41:00
此XPath表达式有一个问题
//*[contains(text(), "›")]/@href/text()因为href属性没有text()属性。
这是一个工作蜘蛛,你可以根据你的需要进行调整:
# -*- coding: utf-8 -*-
import scrapy
class GenyInterimSpider(scrapy.Spider):
name = 'geny-interim'
start_urls = ['http://www.geny-interim.com/offres/']
def parse(self, response):
for offer in response.xpath('//div[contains(@class,"featured-box")]'):
yield {
'title': offer.xpath('.//h3/a/text()').extract_first()
}
next_page_url = response.xpath('//a[@class="page" and contains(.,"›")]/@href').extract_first()
if next_page_url:
yield scrapy.Request(response.urljoin(next_page_url), callback=self.parse)https://stackoverflow.com/questions/55889364
复制相似问题