python - xpath 不包含 A 和 B

标签 python xpath scrapy

如何添加 not(contains(.,'facebook'), not(contains(.,'twitter') 到我的 xpath。

sites = selector.xpath("//h3[@class='r']/a[@href[not(contains(.,'google')   )]]/@href")

我想找到一个没有 google,facebook, and twitter 的 url 请帮助我,谢谢

最佳答案

您可以使用加入条件:

//h3[@class='r']/a[not(contains(@href,'google')) and not(contains(@href,'facebook')) and not(contains(@href,'twitter'))]/@href")

或者,使用 .re() methodSelector 实例上可用:

selector.xpath("//h3[@class='r']/a/@href").re('^(?!.*(google|facebook|twitter)).*$')

此外,您可以使用 re:test() function :

selector.xpath("//h3[@class='r']/a[not(re:test(@href, '(google|facebook|twitter)'))]/@href")

关于python - xpath 不包含 A 和 B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163626/

相关文章:

python - 在 Pandas 中反转 'one-hot' 编码

python django 休息框架。如何以某种特定格式序列化外键 UUID?

python - 如何查看在 automator 中运行的脚本的标准输出

c# - 如何通过在XmlDocument或XDocument中传递xElement名称来获取绝对xpath?

python - 使用 python 和 scrapy 删除第一个标签 html

python - 从数据框中获取索引作为日期时间对象

php - 警告:DOMXPath::query():表达式无效

xpath - 使用 XPath 查找节点的位置

python - 使用scrapy抓取页面

python - Scrapy 不产生请求