我正在尝试为像这样的HTML中的所有图像提取源
<li class="carousel_item">
<img src="http://www.xxxx.com/1fc1c1c2db5852e08ffcff38.jpg"
width="200"
height="200"
style="margin-bottom:1px"
class="image_error" />
<span class="image_error" title="jpg"></span>
<span class="image_error" title="2.jpg"></span>
</li>
当我使用以下命令时,我会获得文档中的所有图像
$nodelist = $xpath->query( "//img/@src" );
foreach ($nodelist as $n){
echo $n->nodeValue."<br>";
}
但我只想要class =“carousel_item”
中列出的项目这可能吗?
答案 0 :(得分:1)
当然,只需在查询中包含<li>
及其class属性即可。您只需将xpath字符串更改为:
$nodelist = $xpath->query( "//li[@class='carousel_item']/img/@src" );
答案 1 :(得分:1)
$nodelist = $xpath->query('//li[contains(@class, "carousel_item")]/img/@src');