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 相关文章推荐
如何使用PHP中的字符串函数
Nov 24 PHP
PHP 函数学习简单小结
Jul 08 PHP
php smarty 二级分类代码和模版循环例子
Jun 01 PHP
php excel reader读取excel内容存入数据库实现代码
Dec 06 PHP
PHP中isset()和unset()函数的用法小结
Mar 11 PHP
php的array数组和使用实例简明教程(容易理解)
Mar 20 PHP
PHP动态柱状图实现方法
Mar 30 PHP
php中JSON的使用方法
Apr 30 PHP
PHP的全局错误处理详解
Apr 25 PHP
php编程实现简单的网页版计算器功能示例
Apr 26 PHP
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
May 15 PHP
详解Laravel框架的依赖注入功能
May 27 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
PHP开发者常犯的10个MySQL错误更正剖析
2012/01/30 PHP
深入解析php中的foreach函数
2013/08/31 PHP
php获取从百度搜索进入网站的关键词的详细代码
2014/01/08 PHP
利用yahoo汇率接口实现实时汇率转换示例 汇率转换器
2014/01/14 PHP
浅析ThinkPHP中execute和query方法的区别
2014/06/13 PHP
CodeIgniter CLI模式简介
2014/06/17 PHP
PHP 7.0新增加的特性介绍
2017/06/08 PHP
Laravel学习教程之request validation的编写
2017/10/25 PHP
Raphael一个用于在网页中绘制矢量图形的Javascript库
2013/01/08 Javascript
关于div自适应高度/左右高度自适应一致的js代码
2013/03/22 Javascript
jQuery中focus事件用法实例
2014/12/26 Javascript
nodejs中实现阻塞实例
2015/03/24 NodeJs
js代码验证手机号码和电话号码是否合法
2015/07/30 Javascript
AngularJS基础 ng-hide 指令用法及示例代码
2016/08/01 Javascript
微信小程序 wxapp内容组件 progress详细介绍
2016/10/31 Javascript
js字符串操作总结(必看篇)
2016/11/22 Javascript
详解Node.js access_token的获取、存储及更新
2017/06/20 Javascript
vue与bootstrap实现时间选择器的示例代码
2017/08/26 Javascript
angular2 ng2-file-upload上传示例代码
2018/08/23 Javascript
js实现图片无缝循环轮播
2019/10/28 Javascript
菜鸟使用python实现正则检测密码合法性
2016/01/05 Python
PyCharm设置SSH远程调试的方法
2018/07/17 Python
Django REST framework 分页的实现代码
2019/06/19 Python
通过python扫描二维码/条形码并打印数据
2019/11/14 Python
Python读入mnist二进制图像文件并显示实例
2020/04/24 Python
PyTorch中Tensor的数据类型和运算的使用
2020/09/03 Python
python爬虫多次请求超时的几种重试方法(6种)
2020/12/01 Python
学校安全检查制度
2014/01/27 职场文书
安全检查管理制度
2014/02/02 职场文书
会计演讲稿范文
2014/05/23 职场文书
土地租赁协议书
2015/01/29 职场文书
收银员岗位职责
2015/02/03 职场文书
上诉答辩状范文
2015/05/22 职场文书
色戒观后感
2015/06/12 职场文书
nginx服务器的下载安装与使用详解
2021/08/02 Servers
Spark SQL 2.4.8 操作 Dataframe的两种方式
2021/10/16 SQL Server