简体   繁体   English

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

[英]XPath Query: get attribute href from a tag

I want to use XPath to get the href attribute from an a -tag, but it has two occurrences within the same file.我想使用 XPath 从a -tag 获取href属性,但它在同一个文件中出现了两次。 How am I getting along?我相处得怎么样? I need to check IF there is an href attribute with value $street/object, I have got this code and it does not work:我需要检查是否有一个值为 $street/object 的href属性,我有这个代码但它不起作用:

$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?谁能帮帮我?

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

The answer shared by @mockinterface is correct. @mockinterface 分享的答案是正确的。 Although I would like to add my 2 cents to it.虽然我想加上我的 2 美分。

If someone is using frameworks like scrapy the you will have to use /html/body//a[contains(@href,'com')][2]/@href along with get() like this:如果有人使用像scrapy的框架,你将不得不像这样使用/html/body//a[contains(@href,'com')][2]/@href和 get() :

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

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

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