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)文章
Laravel?Eloquent的九個(gè)實(shí)用特性詳解
這篇文章主要為大家介紹了LaravelEloquent的九個(gè)實(shí)用特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
ThinkPHP5集成JS-SDK實(shí)現(xiàn)微信自定義分享功能
這篇文章主要介紹了ThinkPHP5集成JS-SDK實(shí)現(xiàn)微信自定義分享功能的相關(guān)資料,需要的朋友可以參考下2023-03-03
PHP ajax+jQuery 實(shí)現(xiàn)批量刪除功能實(shí)例代碼小結(jié)
這篇文章主要介紹了PHP ajax+jQuery 實(shí)現(xiàn)批量刪除功能實(shí)例代碼小結(jié),代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
微信公眾平臺(tái)開(kāi)發(fā)之配置與請(qǐng)求
微信公眾平臺(tái)是騰訊公司在微信的基礎(chǔ)上新增的功能模塊,通過(guò)這一平臺(tái),個(gè)人和企業(yè)都可以打造一個(gè)微信的公眾號(hào),并實(shí)現(xiàn)和特定群體的文字、圖片、語(yǔ)音的全方位溝通、互動(dòng)。本文給大家介紹微信公眾平臺(tái)開(kāi)發(fā)之配置與請(qǐng)求,需要的朋友可以參考下2015-08-08
thinkphp 框架數(shù)據(jù)庫(kù)切換實(shí)現(xiàn)方法分析
這篇文章主要介紹了thinkphp 框架數(shù)據(jù)庫(kù)切換實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkphp 框架數(shù)據(jù)庫(kù)切換實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
淺談Laravel模板實(shí)體轉(zhuǎn)義帶來(lái)的坑
今天小編就為大家分享一篇淺談Laravel模板實(shí)體轉(zhuǎn)義帶來(lái)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

