【问题标题】:How can I paginate the web pages of the following kind?如何对以下类型的网页进行分页?
【发布时间】:2019-04-28 10:31:33
【问题描述】:

我正在尝试对该站点的页面进行分页 (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()

但这也会产生价值错误。任何帮助都会被赞成。

【问题讨论】:

  • 所有页面是什么意思?您的意思是爬取网站还是仅查找页面上发布的所有职位列表?
  • 我的意思是访问每个页面(分页)

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


【解决方案1】:

这个 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2021-04-15
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多