use*_*713 36 html xpath domdocument
我想使用XPath href
从a
-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]");
Run Code Online (Sandbox Code Playgroud)
它根本不会返回任何东西.谁能帮助我?
moc*_*ace 75
对于以下HTML文档:
<html>
<body>
<a href="http://www.example.com">Example</a>
<a href="http://www.stackoverflow.com">SO</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
xpath查询/html/body//a/@href
(或简单地//a/@href
)将返回:
http://www.example.com http://www.stackoverflow.com
要选择特定的实例使用/html/body//a[N]/@href
,
$ /html/body//a[2]/@href http://www.stackoverflow.com
要测试属性中包含的字符串并返回属性本身,请检查不在属性上的标记:
$ /html/body//a[contains(@href,'example')]/@href http://www.example.com
混合两者:
$ /html/body//a[contains(@href,'com')][2]/@href http://www.stackoverflow.com
@mockinterface 分享的答案是正确的。虽然我想加上我的 2 美分。
如果有人使用像这样的框架,scrapy
您将必须/html/body//a[contains(@href,'com')][2]/@href
与 get() 一起使用,如下所示:
response.xpath('//a[contains(@href,'com')][2]/@href').get()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
76736 次 |
最近记录: |