Html XPath 查询:从标签中获取属性 href

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21455349/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:45:57  来源:igfitidea点击:

XPath Query: get attribute href from a tag

htmlxpathdomdocument

提问by user3239713

I want to use XPath to get the hrefattribute from an a-tag, but it has two occurrences within the same file. How am I getting along? I need to check IF there is an hrefattribute with value $street/object, I have got this code and it does not work:

我想使用 XPathhrefa-tag获取属性,但它在同一个文件中出现了两次。我相处得怎么样?我需要检查是否有href值为 $street/object的属性,我有这个代码但它不起作用:

$product_photo     = $xpath->query("//a[contains(@href,'{$object_street}fotos/')][1]");
        $product_360       = $xpath->query("//a[contains(@href,'{$object_street}360-fotos/')][1]");
        $product_blueprint = $xpath->query("//a[contains(@href,'{$object_street}plattegrond/')][1]");
        $product_video     = $xpath->query("//a[contains(@href,'{$object_street}video/')][1]");

It does not return anything at all. Who can help me out?

它根本不返回任何东西。谁能帮帮我?

回答by mockinterface

For the following HTML document:

对于以下 HTML 文档:

<html>
  <body>
    <a href="http://www.example.com">Example</a> 
    <a href="http://www.stackoverflow.com">SO</a> 
  </body>
</html>

The xpath query /html/body//a/@href(or simply //a/@href) will return:

xpath 查询/html/body//a/@href(或简单地//a/@href)将返回:

    http://www.example.com
    http://www.stackoverflow.com

To select a specific instance use /html/body//a[N]/@href,

要选择特定的实例使用/html/body//a[N]/@href

    $ /html/body//a[2]/@href
    http://www.stackoverflow.com

To test for strings contained in the attribute and return the attribute itself place the check on the tag not on the attribute:

要测试属性中包含的字符串并返回属性本身,请检查标签而不是属性:

    $ /html/body//a[contains(@href,'example')]/@href
    http://www.example.com

Mixing the two:

两者混合:

    $ /html/body//a[contains(@href,'com')][2]/@href
    http://www.stackoverflow.com

回答by Rahul Saxena

The answer shared by @mockinterface is correct. Although I would like to add my 2 cents to it.

@mockinterface 分享的答案是正确的。虽然我想加上我的 2 美分。

If someone is using frameworks like scrapythe you will have to use /html/body//a[contains(@href,'com')][2]/@hrefalong with get() like this:

如果有人使用像scrapy你这样的框架,你将不得不/html/body//a[contains(@href,'com')][2]/@href像这样使用 get() :

response.xpath('//a[contains(@href,'com')][2]/@href').get()