【问题标题】:PHP - Simple HTML Dom ParserPHP - 简单的 HTML Dom 解析器
【发布时间】:2012-01-17 10:31:36
【问题描述】:

我正在尝试使用 PHP Simple HTML Dom Parser 从网站检索一些信息。在网站上有许多带有“forumborder2”类的表格,在其中我想获取一些信息。在下一个示例中,我想要图像源。

<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-&gt;children(1)-&gt;children(1) 我可以得到&lt;img src="modules/Requests/images/fPortugal.png" width=18 height=12 border=0 alt='' title=''&gt; 那为什么我不能访问 src 属性。

【问题讨论】:

  • 什么是“简单的 HTML dom 解析器”? 什么错误???
  • @Jon, PHP Simple HTML DOM Parser。也更改了错误的帖子。

标签: php simple-html-dom


【解决方案1】:

您不能使用 HTML Dom Parser 获取所有图像吗?我不确定您是否只是从 HTML 的某个部分获取它,但如果是,您可以在图像源上运行正则表达式以获取您正在寻找的图像;这是一个可能有帮助的代码 sn-p:

// 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>';

希望对你有帮助

【讨论】:

  • 那将是我的第二个选择。我宁愿在遍历每个“forumborder2”表时进行一些处理,而不是稍后通过遍历“图像数组”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 2016-07-30
  • 1970-01-01
  • 2013-03-23
  • 2014-01-15
  • 2011-08-27
相关资源
最近更新 更多