php實(shí)現(xiàn)的一個(gè)很好用HTML解析器類可用于采集數(shù)據(jù)
更新時(shí)間:2013年09月23日 17:24:33 作者:
下面就是這個(gè) HTML解析類及用法,下面的功能是采集www.opendir.cn這個(gè)網(wǎng)站的百度收錄數(shù)據(jù),需要的朋友可以測(cè)試下
復(fù)制代碼 代碼如下:
<?php
$oldSetting = libxml_use_internal_errors( true );
libxml_clear_errors();
/**
*
* -+-----------------------------------
* |PHP5 Framework - 2011
* |Web Site: www.iblue.cc
* |E-mail: mejinke@gmail.com
* |Date: 2012-10-12
* -+-----------------------------------
*
* @desc HTML解析器
* @author jingke
*/
class XF_HtmlDom
{
private $_xpath = null;
private $_nodePath = '';
public function __construct($xpath = null, $nodePath = '')
{
$this->_xpath = $xpath;
$this->_nodePath = $nodePath;
}
public function loadHtml($url)
{
ini_set('user_agent', 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 –Nexus');
$content = '';
if(strpos(strtolower($url), 'http')===false)
{
$content = file_get_contents($url);
}
else
{
$ch = curl_init();
$user_agent = "Baiduspider+(+http://www.baidu.com/search/spider.htm)";
$user_agent1='Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
$content =curl_exec($ch);
curl_close($ch);
}
$html = new DOMDocument();
$html->loadHtml($content);
$this->_xpath = new DOMXPath( $html );
//return $this;
}
public function find($query, $index = null)
{
if($this->_nodePath == '')
$this->_nodePath = '//';
else
$this->_nodePath .= '/';
$nodes = $this->_xpath->query($this->_nodePath.$query);
//echo $nodes->item(0)->getNodePath();exit;
if ($index == null && !is_numeric($index))
{
$tmp = array();
foreach ($nodes as $node)
{
$tmp[] = new XF_HtmlDom($this->_xpath, $node->getNodePath());
}
return $tmp;
}
return new XF_HtmlDom($this->_xpath,$this->_xpath->query($this->_nodePath.$query)->item($index)->getNodePath());
}
/**
* 獲取內(nèi)容
*/
public function text()
{
if ($this->_nodePath != '' && $this->_xpath != null )
return $this->_xpath->query($this->_nodePath)->item(0)->textContent;
else
return false;
}
/**
* 獲取屬性值
*/
public function getAttribute($name)
{
if ($this->_nodePath != '' && $this->_xpath != null )
return $this->_xpath->query($this->_nodePath)->item(0)->getAttribute($name);
else
return false;
}
public function __get($name)
{
if($name == 'innertext')
return $this->text();
else
return $this->getAttribute($name);
}
}
$xp = new xf_HtmlDom();
$xp->loadHtml('http://www.aizhan.com/siteall/www.opendir.cn/');
$rows = $xp->find("td[@id='baidu']/a", 0)->innertext;
print_r($rows);
您可能感興趣的文章:
- PHP解析html類庫(kù)simple_html_dom的轉(zhuǎn)碼bug
- php解析html類庫(kù)simple_html_dom(詳細(xì)介紹)
- 淺析php插件 Simple HTML DOM 用DOM方式處理HTML
- PHP simple_html_dom.php+正則 采集文章代碼
- WordPress中轉(zhuǎn)義HTML與過(guò)濾鏈接的相關(guān)PHP函數(shù)使用解析
- php基于Snoopy解析網(wǎng)頁(yè)html的方法
- PHP抓取網(wǎng)頁(yè)、解析HTML常用的方法總結(jié)
- 淺析php插件 HTMLPurifier HTML解析器
- 解析關(guān)于java,php以及html的所有文件編碼與亂碼的處理方法匯總
- 解析PHP生成靜態(tài)html文件的三種方法
- 用php解析html的實(shí)現(xiàn)代碼
- php使用simple_html_dom解析HTML示例
相關(guān)文章
提交表單后 PHP獲取提交內(nèi)容的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇提交表單后 PHP獲取提交內(nèi)容的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05php過(guò)濾html中的其他網(wǎng)站鏈接的方法(域名白名單功能)
這篇文章主要介紹了php過(guò)濾html中的其他網(wǎng)站鏈接的方法(域名白名單功能),需要的朋友可以參考下2014-04-04利用laravel搭建一個(gè)迷你博客實(shí)戰(zhàn)教程
這篇文章主要給大家介紹了關(guān)于利用laravel搭建一個(gè)迷你博客的相關(guān)資料,文中將一步步的實(shí)現(xiàn)步驟通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08php中實(shí)現(xiàn)用數(shù)組嫵媚地生成要執(zhí)行的sql語(yǔ)句
這篇文章主要介紹了php中實(shí)現(xiàn)用數(shù)組嫵媚地生成要執(zhí)行的sql語(yǔ)句,本文直接給出代碼示例,需要的朋友可以參考下2015-07-07CodeIgniter實(shí)現(xiàn)更改view文件夾路徑的方法
這篇文章主要介紹了CodeIgniter實(shí)現(xiàn)更改view文件夾路徑的方法,需要的朋友可以參考下2014-07-07基于php解決json_encode中文UNICODE轉(zhuǎn)碼問(wèn)題
這篇文章主要介紹了基于php解決json_encode中文UNICODE轉(zhuǎn)碼問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11在win系統(tǒng)安裝配置 Memcached for PHP 5.3 圖文教程
這篇文章主要介紹了在win系統(tǒng)安裝配置 Memcached for PHP 5.3 圖文教程,需要的朋友可以參考下2015-03-03