php中的Xpath和图像


Xpath and images in php

试图使用Xpath从网站上抓取图像和数据,但一直显示错误。有人能帮我吗。

 <?php
$dom = new DOMDocument();
@$dom->loadHTMLFile('http://www.hassconsult.co.ke/index.php?option=com_content&view=article&id=22&Itemid=29');
$xpath = new DOMXPath($dom);
foreach($xpath->query("//div[@id='ad']/table") as $table) {
  echo $xpath->query(".//img[@align='centre']", $table)->item(0)->nodeValue . "'n";// should come in here don't know what to put 
  echo $xpath->query(".//span[@class='style8']", $table)->item(0)->nodeValue."'n";
  echo $xpath->query(".//div[@class='style10']/div", $table)->item(0)->nodeValue."'n";
  echo $xpath->query("//div[@align='justify']", $table)->item(0)->nodeValue. "'n";
}
?>

这是购买firebug 的轮廓

 <td>
<div align="center" style="border:1px #007AC7 solid;width:199px;height:131px;">
<a href="/index.php?option=com_content&view=article&id=27&Itemid=74&send=5&ref_no=834/II">
<img width="199" height="131" border="0" style="border:1px #007AC7 solid;" alt="Photo" src="/images/markers/l_569.jpg">
</a>
</div>
</td>

您所指的示例没有任何属性为align='centre'IMG标记——如果删除[@align='centre'],则可以使用Shikiryu所指示的getAttribute('src'),例如

echo $xpath->query(".//img", $table)->item(0)->getAttribute('src'). "'n";

正如我在评论中所说:属性:

echo $xpath->query(".//img[@align='centre']", $table)->item(0)->getAttribute('src'). "'n";