[英]How can I get img src using php domxpath
I want to get the img src value using domxpath. 我想使用domxpath获得img src值。
Let us say I have this sample.html page: 让我们说我有这个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>
Using CURL, DOMDocument and DOMXPath I want to get the img src and the title: 使用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;
This will result to img as null: 这将导致img为null:
[
{
"img": null,
"title": "Sample 1"
},
{
"img": null,
"title": "Sample 2"
},
{
"img": null,
"title": "Sample 3"
}
]
Please help me on how to get the img src value. 请帮助我如何获得img src值。 Thank You! 谢谢!
Your XPath in fetching the values isn't quite correct, should be... 您的XPath获取值不太正确,应该是...
$result['img'] = $xpath->query("//img/@src", $entry)[0]->value;
$result['title'] = $xpath->query("//p[@class='title']", $entry)[0]->nodeValue;
Note the way you get the attributes is by using @attibuteName
. 注意,获取属性的方法是使用@attibuteName
。 Also the //
at the start allows XPath to find the elements at any point under the start point. 此外, //
在开始处//
允许XPath在起点下方的任何位置查找元素。
You can get attribute of any DOM element using method getAttribute('attribute_name')
您可以使用方法getAttribute('attribute_name')
获取任何DOM元素getAttribute('attribute_name')
Here in your example use getAttribute('src')
在您的示例中,使用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.