doulai2573 2012-03-25 09:40
浏览 79
已采纳

PHP中的Xpath和图像

Trying to scrape images and data from a website using Xpath but keeps showing me an error.Could anybody help me out.

 <?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 . "
";// should come in here don't know what to put 
  echo $xpath->query(".//span[@class='style8']", $table)->item(0)->nodeValue."
";
  echo $xpath->query(".//div[@class='style10']/div", $table)->item(0)->nodeValue."
";

  echo $xpath->query("//div[@align='justify']", $table)->item(0)->nodeValue. "
";
}

?>

This is the outline as given buy 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>
  • 写回答

2条回答 默认 最新

  • dongyulian5801 2012-03-25 10:07
    关注

    the example you pointed to doesn't have any IMG tags with attribute align='centre' -- if you remove [@align='centre'] you'll be able to use getAttribute('src') as Shikiryu indicates, e.g.

    echo $xpath->query(".//img", $table)->item(0)->getAttribute('src'). "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来