首页 > 解决方案 > 下一页链接的 CSS 选择器在 Scrapy shell 中返回空列表

问题描述

我是 Scrapy 的新手。我尝试从该站点获取指向下一页的链接https://book24.ru/knigi-bestsellery/?section_id=1592

html 是什么样子的:在此处输入图像描述

在scrapy shell中我写了这个命令:

response.css('li.pagination__button-item._next a::attr(href)')

它返回一个空列表。

我也试过

response.css('a.pagination__item._link._button._next.smartLink')

但它也返回一个空列表。

我将不胜感激!

标签: cssscrapyhrefscrapy-shell

解决方案


该页面是使用 JavaScript 生成的,使用“view(response)”查看它的外观。

# with css:
In [1]: response.css('head > link:nth-child(28) ::attr(href)').get()                                                   
Out[1]: 'https://book24.ru/knigi-bestsellery/page-2/'

# with xpath:
In [2]: response.xpath('//link[@rel="next"]/@href').get()
Out[2]: 'https://book24.ru/knigi-bestsellery/page-2/'

推荐阅读