1. 程式人生 > >PHP使用DomDocument抓取HTML內容

PHP使用DomDocument抓取HTML內容

有時候會有需要從一個HTML頁面來分離出你需要的一些資料來進行處理。

當然自己分析檔案肯定可以,但是比較快速且方便的是使用正則表示式或者DOM。

鑑於正則表示式我不熟悉,所以我打算使用DOM來完成。

先談談我的需求,我要從一個HTML頁面的一個表格中提取資料並且將這個資料整理出來加入到MySQL資料庫中。

假設目標HTML中我感興趣的Table有3列,分別是ID,Name,內容。

index.php;

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    $urlTarget = "http://www.xxxx.com/targethtmlpage.html";

    require_once('ContentManager.php');

    //建立Dom物件,分析HTML檔案;
    $htmDoc = new DOMDocument;
    $htmDoc->loadHTMLFile($urlTarget );
    $htmDoc->normalizeDocument();

    //獲得到此文件中每一個Table物件;
    $tables_list = $htmDoc->getElementsByTagName('table');   

    //測試Table Count;
    $tables_count = $tables_list->length;
    foreach ($tables_list as $table)
    {
        //得到Table物件的class屬性
        $tableProp = $table->getAttribute('class');
        if ($tableProp == 'target_table_class')
        {
            $contentMgr = new ContentManager();
            $contentMgr->ParseFromDOMElement($table);
            
            //這裡myParser就完成了分析動作。然後就可以進行需要的操作了。
            //比如寫入MySQL。
            $contentMgr->SerializeToDB();
        }
    }
?>

 


ContentManager.php

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ContentParser
 *
 * @author xxxxx
 */
require_once('ContentInfo.php');
class ContentManager {
    //put your code here
    var $ContentList;
    public function __construct() {
        $this->ContentList = new ArrayObject();
    }
    
    public function ParseFromDOMElement(DOMElement $table)
    {
        $rows_list = $fundsTable->getElementsByTagName('tr');
        $rows_length = $rows_list->length;
        $index = 0;

        foreach ($rows_list as $row)
        {
            $contentInfo = new ContentInfo();
            $contentInfo->ParseFromDOMElement($row);
            $this->ContentList->append ($contentInfo);
        }

        //test how many contents parsed.
        $count = $this->fundsInfoArray->count();
        echo $count; 
    }
    
    public function SerializeToDB()
    {
        //寫入資料庫,程式碼略。
    }
}

?>


contentinfo.php

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ContentInfo
 *
 * @author xxxxx
 */
class ContentInfo {
    //put your code here
    var $ID;
    var $Name;
    var $Content;
    public function ParseFromDOMElement(DOMElement $row)
    {
        $cells_list = $row->getElementsByTagName('td');
        $cells_length = $row->length;
        
        $curCellIdx = 0;
        foreach ($cells_list as $cell)
        {
            switch ($curCellIdx++)
            {
                case 0:
                    $this->ID = $cell->nodeValue;
                    break;
                case 1:
                    $this->Name = $cell->nodeValue;
                    break;
                case 2:
                    $this->Content = $cell->nodeValue;
                    break;
            }
        }
    }
}

?>


一點小心得,DOM中每個Element都可以getAttribute取出屬性,這些屬性可以區分你分析的DOMObject。

舉例來說,比如上述我分析的Target HTML有很多表格,但是我發現目標表格的class屬性和其他表格是不一樣的。

所以,這個屬性就可以來區分我要分析的是哪個表格。

當然更多DOM的東西,大家可以去參考PHP Manual或者是,用IDE(NetBeans7.0就可以)轉到類宣告,看類介面。

有方法使用說明以及引數說明。可以參考參考。