首页 文章

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

提问于
浏览
3

我想在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中有更好的方法来实现这一目标吗?

1 回答

  • 3
    .select('//a[. != "" and translate(., "0123456789", "") = ""]/@href')
    

相关问题