php实现的一个很好用HTML解析器类可用于采集数据


Posted in PHP onSeptember 23, 2013
<?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());   
    }   
    /** 
     * 获取内容 
     */  
    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 相关文章推荐
简单的页面缓冲技术
Oct 09 PHP
php include的妙用,实现路径加密
Jul 29 PHP
解析百度搜索结果link?url=参数分析 (全)
Oct 09 PHP
解析PHP中一些可能会被忽略的问题
Jun 21 PHP
CodeIgniter框架中_remap()使用方法2例
Mar 10 PHP
ThinkPHP后台首页index使用frameset时的注意事项分析
Aug 22 PHP
深入理解PHP中的Streams工具
Jul 03 PHP
PHP使用ODBC连接数据库的方法
Jul 18 PHP
php cookie工作原理与实例详解
Jul 18 PHP
基于php实现的php代码加密解密类完整实例
Oct 12 PHP
PHP高精确度运算BC函数库实例详解
Aug 15 PHP
thinkphp5框架API token身份验证功能示例
May 21 PHP
PHP加密函数 Javascript/Js 解密函数
Sep 23 #PHP
配置php网页显示各种语法错误
Sep 23 #PHP
php函数间的参数传递(值传递/引用传递)
Sep 23 #PHP
php数据类型判断函数有哪些
Sep 23 #PHP
PHP保留两位小数并且四舍五入及不四舍五入的方法
Sep 22 #PHP
php一次性删除前台checkbox多选内容的方法
Sep 22 #PHP
PHP导航下拉菜单的实现如此简单
Sep 22 #PHP
You might like
桌面中心(三)修改数据库
2006/10/09 PHP
Mysql数据库操作类( 1127版,提供源码下载 )
2010/12/02 PHP
php设计模式 Bridge (桥接模式)
2011/06/26 PHP
php设计模式 Proxy (代理模式)
2011/06/26 PHP
PHP 5.3和PHP 5.4出现FastCGI Error解决方法
2015/02/12 PHP
PHP jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图
2017/05/15 PHP
php微信公众号开发之快递查询
2018/10/20 PHP
精通Javascript系列之数值计算
2011/06/07 Javascript
Bootstrap每天必学之媒体对象
2015/11/30 Javascript
使用Ajax与服务器(JSON)通信实例
2016/11/04 Javascript
微信小程序中hidden不生效原因的解决办法
2017/04/26 Javascript
想用好React的你必须要知道的一些事情
2017/07/24 Javascript
react-native 圆弧拖动进度条实现的示例代码
2018/04/12 Javascript
记录一篇关于redux-saga的基本使用过程
2018/08/18 Javascript
详解vue使用插槽分发内容slot的用法
2019/03/28 Javascript
解决微信小程序scroll-view组件无横向滚动的问题
2020/02/04 Javascript
简单介绍Python中的try和finally和with方法
2015/05/05 Python
在Ubuntu系统下安装使用Python的GUI工具wxPython
2016/02/18 Python
Python实现通讯录功能
2018/02/22 Python
python3将视频流保存为本地视频文件
2018/06/20 Python
如何利用python检测图片是否包含二维码
2020/10/15 Python
OSPREY LONDON官网:英国本土皮具品牌
2019/05/31 全球购物
经典c++面试题四
2015/05/14 面试题
Prototype中如何为一个元素添加一个方法
2014/12/08 面试题
出口公司经理求职简历中的自我评价
2013/10/13 职场文书
毕业生求职简历中的自我评价
2013/10/18 职场文书
高级销售员求职信
2013/10/25 职场文书
总经理岗位职责
2013/11/09 职场文书
数据员岗位职责
2013/11/19 职场文书
自我评价的范文
2014/02/02 职场文书
工程力学专业自荐信范文
2014/03/17 职场文书
优秀教师先进个人事迹材料
2014/08/31 职场文书
机票销售员态度不好检讨书
2014/09/27 职场文书
工伤劳动仲裁代理词
2015/05/25 职场文书
MySQL如何使用使用Xtrabackup进行备份和恢复
2021/06/21 MySQL
MySQL导致索引失效的几种情况
2022/06/25 MySQL