我该如何对以下类型的网页进行分页?

时间:2019-04-28 10:31:33

标签: python-2.7 scrapy ascii non-ascii-characters

我正在尝试对本网站(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()

但是这也会产生值错误。任何帮助都将被否决。

1 个答案:

答案 0 :(得分:0)

此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)