2011-12-10 149 views
0

我试图从使用PHP简单的HTML Dom解析器的网站检索一些信息。在网站上有很多表“forumborder2”的表格,里面我想获得一些信息。在下一个例子中,我想要图像源。PHP - 简单的HTML Dom解析器

<table class="forumborder2" width=100% cellspacing=0 cellpadding=0 border=0 align=center> 
     <tr> 
      <td class="titleoverallheader2" background="modules/Forums/templates/chunkstyle/imagesnew/forumtop.jpg" style="border-top:0px; border-left:0px; border-right:0px;" width="100%" colspan=7 Align=left> 
       <b>Supernatural </b>(2005)&nbsp;&nbsp; - &nbsp;&nbsp;Enviada por: <b>AlJoSi</b>&nbsp;&nbsp; em 6 de Dezembro, 2011 (22:35:11) 
      </td> 
     </tr> 
     <tr height=25> 
      <td class="colour12" align=right width=100> 
       <b>Idioma:</b> 
      </td> 
      <td width=110 class="colour22"> 
       <img src="modules/Requests/images/fPortugal.png" width=18 height=12 border=0 alt='' title=''> 
      </td> 
     </tr> </table> 

我做了以下内容:

foreach($html->find('table[class="forumborder2"]')as $tr){ 
    echo $tr->children(1)->children(1)->src; } 

这总是给人错误:“试图获得非对象的属性”。如果我只去$tr->children(1)->children(1)我可以得到<img src="modules/Requests/images/fPortugal.png" width=18 height=12 border=0 alt='' title=''>为什么我不能访问src属性。

+0

什么是“简单的HTML DOM解析器”?和**什么错误**? – Jon

+0

@Jon,[PHP简单HTML DOM解析器](http://simplehtmldom.sourceforge.net/)。也改变了错误的帖子。 – Barata

回答

4

难道你不能只使用HTML Dom解析器获取所有图像吗?我不确定是否只是从HTML的某个部分抓取它,但如果是的话,您可以在图像源上运行一个正则表达式以获得您要查找的正则表达式;这里有一个代码片断,可以帮助:

// Create DOM from URL or file 
$html = file_get_html('http://www.google.com/'); 

// Find all images 
foreach($html->find('img') as $element) 
    echo $element->src . '<br>'; 

// Find all links 
foreach($html->find('a') as $element) 
    echo $element->href . '<br>'; 

我希望这有助于

+0

这将是我的第二选择。我宁愿做一些处理,因为我遍历每个“forumborder2”表,而不是稍后迭代通过“图像数组”。 – Barata

相关问题