用php解析html的實(shí)現(xiàn)代碼
更新時(shí)間:2011年08月08日 21:45:32 作者:
用php解析html的實(shí)現(xiàn)代碼,一般做采集的朋友需要用得到。
最近想用php寫一個(gè)爬蟲,就需要解析html,在sourceforge上找到一個(gè)項(xiàng)目叫做PHP Simple HTML DOM Parser,它可以以類似jQuery的方式通過css選擇器來返回指定的DOM元素,功能十分強(qiáng)大。
首先要在程序的開始引入simple_html_dom.php這個(gè)文件
include_once('simple_html_dom.php');
PHP Simple HTML DOM Parser提供了3種方式來創(chuàng)建DOM對象
// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');
// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
得到DOM對象后就可以進(jìn)行各種操作了
// Find all anchors, returns a array of element objects
$ret = $html->find('a');
// Find (N)th anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', 0);
// Find lastest anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', -1);
// Find all <div> with the id attribute
$ret = $html->find('div[id]');
// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]');
這里可以使用各種css選擇器,就像在jQuery中進(jìn)行DOM操作一樣,非常方便。此外,還有兩個(gè)特殊的屬性可以得到文本和注釋的內(nèi)容
// Find all text blocks
$es = $html->find('text');
// Find all comment (<!--...-->) blocks
$es = $html->find('comment');
當(dāng)然,還是類似于jQuery,PHP Simple HTML DOM Parser也支持鏈?zhǔn)讲僮?,以及各種訪問DOM元素的簡單方法
// Example
echo $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;
// or
echo $html->getElementById("div1")->childNodes(1)->childNodes(1)->childNodes(2)->getAttribute('id');
首先要在程序的開始引入simple_html_dom.php這個(gè)文件
復(fù)制代碼 代碼如下:
include_once('simple_html_dom.php');
PHP Simple HTML DOM Parser提供了3種方式來創(chuàng)建DOM對象
復(fù)制代碼 代碼如下:
// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');
// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
得到DOM對象后就可以進(jìn)行各種操作了
復(fù)制代碼 代碼如下:
// Find all anchors, returns a array of element objects
$ret = $html->find('a');
// Find (N)th anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', 0);
// Find lastest anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', -1);
// Find all <div> with the id attribute
$ret = $html->find('div[id]');
// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]');
這里可以使用各種css選擇器,就像在jQuery中進(jìn)行DOM操作一樣,非常方便。此外,還有兩個(gè)特殊的屬性可以得到文本和注釋的內(nèi)容
復(fù)制代碼 代碼如下:
// Find all text blocks
$es = $html->find('text');
// Find all comment (<!--...-->) blocks
$es = $html->find('comment');
當(dāng)然,還是類似于jQuery,PHP Simple HTML DOM Parser也支持鏈?zhǔn)讲僮?,以及各種訪問DOM元素的簡單方法
復(fù)制代碼 代碼如下:
// Example
echo $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;
// or
echo $html->getElementById("div1")->childNodes(1)->childNodes(1)->childNodes(2)->getAttribute('id');
您可能感興趣的文章:
- PHP解析html類庫simple_html_dom的轉(zhuǎn)碼bug
- php解析html類庫simple_html_dom(詳細(xì)介紹)
- 淺析php插件 Simple HTML DOM 用DOM方式處理HTML
- PHP simple_html_dom.php+正則 采集文章代碼
- WordPress中轉(zhuǎn)義HTML與過濾鏈接的相關(guān)PHP函數(shù)使用解析
- php基于Snoopy解析網(wǎng)頁html的方法
- PHP抓取網(wǎng)頁、解析HTML常用的方法總結(jié)
- php實(shí)現(xiàn)的一個(gè)很好用HTML解析器類可用于采集數(shù)據(jù)
- 淺析php插件 HTMLPurifier HTML解析器
- 解析關(guān)于java,php以及html的所有文件編碼與亂碼的處理方法匯總
- 解析PHP生成靜態(tài)html文件的三種方法
- php使用simple_html_dom解析HTML示例
相關(guān)文章
PHP版 漢字轉(zhuǎn)碼的實(shí)現(xiàn)詳解
本篇文章是對用php實(shí)現(xiàn)漢字轉(zhuǎn)碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
在php7中MongoDB實(shí)現(xiàn)模糊查詢的方法詳解
MongoDB模糊查詢語句相信對大家來說都不陌生,這篇文章主要給大家介紹了在php 7中MongoDB實(shí)現(xiàn)模糊查詢的方法,文中給出了詳細(xì)的介紹和示例代碼,對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友一起來看看吧。2017-05-05
php 處理上百萬條的數(shù)據(jù)庫如何提高處理查詢速度
php 處理上百萬條的數(shù)據(jù)庫如何提高處理查詢速度2010-02-02
PHP的Laravel框架中使用AdminLTE模板來編寫網(wǎng)站后臺界面
這篇文章主要介紹了PHP的Laravel框架中使用AdminLTE模板來編寫網(wǎng)站后臺的方法,AdminLTE基于BootStrap,能幫助快速創(chuàng)建網(wǎng)站后臺管理面板界面,需要的朋友可以參考下2016-03-03

