链接中包含特定关键字的 href 的 xpath

Bil*_*hon 4 python xpath scrapy

我需要的是找到页面上所有在链接本身中有一些关键字的链接。因此,基于一些堆栈主题,我按如下方式构建我的 xpath:

response.xpath('//a[contains(@href,'/Best-Sellers-Health-Personal-Care')]')
Run Code Online (Sandbox Code Playgroud)

这应该返回一个链接,如 =“ https://www.amazon.com/Best-Sellers-Health-Personal-Care-Tongue ......”

但我总是收到无效的语法错误。我错了什么?所以我现在所做的只是在遍历列表时添加 if contains 检查。但希望有更优雅、更快速的解决方案。

And*_*son 8

这是因为引号的使用不一致。

只需更换

'//a[contains(@href,'/Best-Sellers-Health-Personal-Care')]'
Run Code Online (Sandbox Code Playgroud)

'//a[contains(@href,"/Best-Sellers-Health-Personal-Care")]'
Run Code Online (Sandbox Code Playgroud)