简体   繁体   English

如何在 XPath 中选择多个标签?

[英]How to select multiple tags in XPath?

one thing I'm trying to implement in my code is a XPath to extract results of 2 different tags, in such a way that the results shown on the screen present these attributes together.我试图在我的代码中实现的一件事是使用 XPath 提取 2 个不同标签的结果,这样屏幕上显示的结果将这些属性一起呈现。

I've tried with union functions for the 2 different XPaths but the results are presented in different lines:我已经尝试使用 2 个不同 XPath 的联合函数,但结果显示在不同的行中:

xpath1(for tag1) | xpath2(for tag2)

would like to have the results this way: results: tag1 , tag2想要这样的结果: results: tag1 , tag2

Any ideas?有任何想法吗? Thanks in advance,提前致谢,

This XPath,这个 XPath,

//*[self::tag1 or self::tag2][ predicate ] //*[self::tag1 or self::tag2][谓词]

will select elements named tag1 or tag2 that also satisfy an additional predicate .将选择名为tag1tag2元素也满足附加谓词

Comma vs line separation of the results is a function of the hosting language/tool, not XPath itself.结果的逗号与行分隔是托管语言/工具的功能,而不是 XPath 本身。

If I understand your question properly you want the result of two tags in one for example list, in that case:如果我正确理解您的问题,您希望在一个列表中包含两个标签的结果,在这种情况下:

response.xpath('//tag1 | //tag2').getall()

you can do this as well.你也可以这样做。

Additionally you can attach any other expressions to both tags.此外,您可以将任何其他表达式附加到两个标签。

ِYou may also want to visit XPath syntax on w3schools . ِ您可能还想访问w3schools 上的 XPath 语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM