xml 如何使用 XPath 通过链接文本找到链接 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/915338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find the link URL by link text with XPath?
提问by flybywire
I have a well formed XHTMLpage. I want to find the destination URL of a link when I have the text that is linked.
我有一个格式良好的XHTML页面。当我有链接的文本时,我想找到链接的目标 URL。
Example
例子
<a href="http://stackoverflow.com">programming questions site</a>
<a href="http://cnn.com">news</a>
I want an XPathexpression such that if given programming questions siteit will give http://stackoverflow.comand if I give it newsit will give http://cnn.com.
我想要一个XPath表达式,如果给出programming questions site它就会给出http://stackoverflow.com,如果我给出news它就会给出http://cnn.com.
回答by Badaro
Should be something similar to:
应该类似于:
//a[text()='text_i_want_to_find']/@href
回答by MaDeuce
Too late for you, but for anyone else with the same question...
对你来说太晚了,但对于其他有同样问题的人......
//a[contains(text(), 'programming')]/@href
Of course, 'programming' can be any text fragment.
当然,“编程”可以是任何文本片段。
回答by Brian Agnew
//a[text()='programming quesions site']/@href
which basically identifies an anchor node <a>that has the text you want, and extracts the hrefattribute.
它基本上标识了一个<a>包含您想要的文本的锚节点,并提取href属性。
回答by Baxter Tidwell
Think of the phrase in the square brackets as a WHERE clause in SQL.
将方括号中的短语视为 SQL 中的 WHERE 子句。
So this query says, "select the "href" attribute (@) of an "a" tag that appears anywhere (//), but only where (the bracketed phrase) the textual contents of the "a" tag is equal to 'programming questions site'".
所以这个查询说,“选择出现在任何地方(//)的“a”标签的“href”属性(@),但仅限于(括号内的短语)“a”标签的文本内容等于'编程问题网站'”。
回答by Abdo
For case insensitive contains, use the following:
对于不区分大小写的包含,请使用以下内容:
//a[contains(translate(text(),'PROGRAMMING','programming'), 'programming')]/@href
translate converts capital letters in PROGRAMMING to lower case programming.
translate 将 PROGRAMMING 中的大写字母转换为小写字母编程。
回答by Miguel Vaz
if you are using html agility pack use getattributeValue:
如果您使用 html 敏捷包,请使用 getattributeValue:
$doc2.DocumentNode.SelectNodes("//div[@class='className']/div[@class='InternalClass']/a[@class='InternalClass']").GetAttributeValue("href","")

