【问题标题】:xpath not contains A and Bxpath 不包含 A 和 B
【发布时间】:2015-01-27 05:09:53
【问题描述】:

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

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

我想找一个没有google,facebook,和twitter的网址 请帮帮我,谢谢

【问题讨论】:

  • 您需要更加小心地明确表达您的要求。我怀疑你想要一个没有 google、twitter 或 facebook 的 url:也就是说,这三个中的任何一个都会取消 URL 的资格,而你编写它的方式,只有当这三个都存在时才会取消资格。

标签: python xpath scrapy


【解决方案1】:

你可以用and加入条件:

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

或者,使用Selector 实例上可用的.re() method

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")

【讨论】:

    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 2019-08-09
    • 2017-01-21
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多