xpath 不包含 A 和 B

use*_*364 5 python xpath scrapy

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

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

我想找到一个不带google, facebook,twitter的网址 请帮助我,谢谢

ale*_*cxe 5

您可以使用以下方式加入条件and

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

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

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

另外,您可以使用re:test()函数

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