提取匹配正则表达式文本的URL - 使用XPath 1.0

3 投票
1 回答
2326 浏览
提问于 2025-04-16 19:51

我想用Scrapy里的XPath提取这种类型的链接(链接文本是一个数字,数字可以有任意位数,而链接地址是随机的文本)。

  • <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中有没有更好的方法来实现这个呢?

1 个回答

暂无回答

撰写回答