我使用 DOMDocument
对象从中获取一些数据:
<div class="prodImg">
<a href="#"><img src="some_image_src"/></a>
</div>
使用这段代码:
libxml_use_internal_errors(true);
$homepage = file_get_contents('some_src');
$doc = new DomDocument;
@$doc->loadHtml($homepage);
$xpath = new DomXpath($doc);
$div = $xpath->query('//*[@class="prodImg"]')->item(0);
我得到了整个 div 容器,但我只想得到图像 src
属性。
最佳答案
对我有用:
$div = $xpath->query('//*[@class="prodImg"]')->item(0)->getElementsByTagName('img')->item(0)->getAttribute('src');
var_dump($div);
产量:
string 'some_image_src' (length=14)
关于php - DOMDocument 获取元素属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29013290/