如何对以下类型的网页进行分页?
How can I paginate the web pages of the following kind?
我正在尝试对本网站的页面进行分页 (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()
但这也是产值错误。任何帮助都会被点赞。
此 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)
我正在尝试对本网站的页面进行分页 (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()
但这也是产值错误。任何帮助都会被点赞。
此 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)