簡體   English   中英

PHP-簡單的HTML Dom解析器

[英]PHP - Simple HTML Dom Parser

我正在嘗試使用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->children(1)->children(1)我可以得到<img src="modules/Requests/images/fPortugal.png" width=18 height=12 border=0 alt='' title=''>所以為什么我不能訪問src屬性。

您不能使用HTML Dom Parser捕獲所有圖像嗎? 我不確定是否只從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>';

我希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM