scrapy - 无法让 xpath 选择“下一步”按钮
问题描述
我正在尝试抓取这个 gem 网站:
发生了一些奇怪的事情,我不知道如何获得按钮href
中的某些元素。Next
例如,
response.xpath('//section')
产量:
[<Selector xpath='//section' data='<section class="specimen-details">\n\t<...'>,
<Selector xpath='//section' data='<section class="specimen-related hidd...'>,
<Selector xpath='//section' data='<section class="shows hidden-print">\n...'>,
<Selector xpath='//section' data='<section class="blog hidden-print">\n ...'>,
<Selector xpath='//section' data='<section class="navigation">\n ...'>]
但是当我在控制台中查看时,我看到了一个<section class="specimen-list">
没有显示在那里并且包含其中的导航按钮的附加信息。我不确定发生了什么事。任何帮助或建议表示赞赏!
解决方案
获取下一页href的xpath是//a[@rel="next"]/@href
所以你基本上可以做
response.xpath('//a[@rel="next"]/@href').get()
或使用 css 选择器
response.css('a[rel="next"]::attr(href)').get()
get() 方法适用于较新版本的scrapy,如果它不适用于您使用的extract_first()。
推荐阅读
- javascript - 转到 Vue.js 中的 $router url 目的地后,如何防止我的 $router 参数消失?
- algorithm - 是否存在一种稳定的排序算法,可以以 O(n) 时间复杂度和 O(1) 辅助空间复杂度对二进制数组进行排序?
- typescript - 从打字稿中的数组中的字符串派生键以定义对象属性名称
- jupyter-notebook - 在 Jupyter 实验室中选择一行是否有键盘快捷键?
- c# - 如何在需要抽象父类型的地方使用子类型
- mongodb - 什么查询在 MongoDB 中使用更少的资源?
- python - 使用 Bash 添加到 Python 路径
- react-native - react-native-community/datetimepicker 没有设置它的所有值
- assembly - 导致表达式语法错误的 mov 指令
- python - Python sys.argv 接受 HTML 输入