![](/img/trans.png)
[英]DomDocument/DOMXPath - How to get HTML Dom element by itemprop and img src
[英]How can I get img src using php domxpath
我想使用domxpath获得img src值。
让我们说我有这个sample.html页面:
<div id="wrapper">
<div class="item">
<div class="img-wrapper">
<img src="sample1.jpg"/>
<p class="title">Sample 1</p>
</div>
</div>
<div class="item">
<div class="img-wrapper">
<img src="sample2.jpg"/>
<p class="title">Sample 2</p>
</div>
</div>
<div class="item">
<div class="img-wrapper">
<img src="sample3.jpg"/>
<p class="title">Sample 3</p>
</div>
</div>
</div>
使用CURL,DOMDocument和DOMXPath,我想获取img src和标题:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://path/to/sample.html');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
$dom->loadHTML($result);
$xpath = new DOMXPath($dom);
$entries = $xpath->query('//div[@id="wrapper"]/div[@class="item"]');
$results = array();
foreach ($entries as $entry) {
$result = array();
$result['img'] = $xpath->query("img", $entry)->item(0)->nodeValue;
$result['title'] = $xpath->query("p[@class='title']", $entry)->item(0)->nodeValue;
$results[] = $result;
}
return $results;
这将导致img为null:
[
{
"img": null,
"title": "Sample 1"
},
{
"img": null,
"title": "Sample 2"
},
{
"img": null,
"title": "Sample 3"
}
]
请帮助我如何获得img src值。 谢谢!
您的XPath获取值不太正确,应该是...
$result['img'] = $xpath->query("//img/@src", $entry)[0]->value;
$result['title'] = $xpath->query("//p[@class='title']", $entry)[0]->nodeValue;
注意,获取属性的方法是使用@attibuteName
。 此外, //
在开始处//
允许XPath在起点下方的任何位置查找元素。
您可以使用方法getAttribute('attribute_name')
获取任何DOM元素getAttribute('attribute_name')
在您的示例中,使用getAttribute('src')
$result['img'] = $xpath->query("//img", $entry)->item(0)->getAttribute('src');
$result['title'] = $xpath->query("//p[@class='title']", $entry)->item(0)->nodeValue;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.