我想在 Scrapy 中使用 XPath 提取这种类型的 URL(链接文本是一个任意位数的数字,href 是一个随机文本)。
-
<a href="http://www.example.com/link_to_some_page.html>3</a>
-
<a href="http://www.example.com/another_link-abcd.html>45</a>
我可以想到类似的东西
HtmlXPathSelector(response).select('//a[matches(text(),"\d+")]/@href')
但是,XPath 2.0 似乎不受支持,我无法使用正则表达式。
我能搜索到的最佳单行解决方案来自这个问题:xpath expression for regex-like matching? - 在 scrapy 中有没有更好的方法来实现这一点?
最佳答案
.select('//a[. != "" and translate(., "0123456789", "") = ""]/@href')
关于python - 提取文本与正则表达式匹配的 URL - 使用 XPath 1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6402980/