简体   繁体   English

PHP-简单的HTML Dom解析器

[英]PHP - Simple HTML Dom Parser

I'm trying to retrieve some information from a website using PHP Simple HTML Dom Parser. 我正在尝试使用PHP Simple HTML Dom Parser从网站检索一些信息。 In the website there are many tables with the class "forumborder2" and inside them i want to get some information. 在网站上,有许多类为“ forumborder2”的表,我想在其中获取一些信息。 In the next example i want the image source. 在下一个示例中,我需要图像源。

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

I did the following: 我做了以下工作:

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

This always gives the error: "Trying to get property of non-object". 这总是产生错误:“试图获取非对象的属性”。 If i only go to $tr->children(1)->children(1) i can get <img src="modules/Requests/images/fPortugal.png" width=18 height=12 border=0 alt='' title=''> so why can't i access the src attribute. 如果我只去$tr->children(1)->children(1)我可以得到<img src="modules/Requests/images/fPortugal.png" width=18 height=12 border=0 alt='' title=''>所以为什么我不能访问src属性。

can't you just grab all the images using the HTML Dom Parser? 您不能使用HTML Dom Parser捕获所有图像吗? I'm not sure if if you're only grabbing it from a certain section of the HTML, but if you are, you could run a regex on the image source to get the ones you're looking for; 我不确定是否只从HTML的某个部分中获取它,但是如果是这样,则可以在图像源上运行一个正则表达式来获取所需的内容。 here's a code snippet that might help: 这是一个可能有帮助的代码段:

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

i hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM