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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?