首页 > 解决方案 > 无法让 xpath 选择“下一步”按钮

问题描述

我正在尝试抓取这个 gem 网站:

https://www.irocks.com/search?_token=q57It5iOxH0R1TpCusPK781faIVHprh47BexHVkM&code=&collection=&description=&interval=&locality=&max=&min=&mode=advanced&name=&operator=%3E%3D&query=&species=&status%5B0%5D=available&status%5B1 %5D=暂停

发生了一些奇怪的事情,我不知道如何获得按钮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">没有显示在那里并且包含其中的导航按钮的附加信息。我不确定发生了什么事。任何帮助或建议表示赞赏!

标签: scrapy

解决方案


获取下一页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()。


推荐阅读