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&amp;&amp;mysql)六
Oct 09 PHP
用PHP连接MySQL代码的参数说明
Jun 07 PHP
php 用checkbox一次性删除多条记录的方法
Feb 23 PHP
php之XML转数组函数的详解
Jun 07 PHP
解析PHP高效率写法(详解原因)
Jun 20 PHP
php的数组与字符串的转换函数整理汇总
Jul 18 PHP
PHP 下载文件时自动添加bom头的方法实例
Jan 10 PHP
php中有关字符串的4个函数substr、strrchr、strstr、ereg介绍和使用例子
Apr 24 PHP
浅谈php冒泡排序
Dec 30 PHP
php中get_defined_constants函数用法实例分析
May 12 PHP
smarty自定义函数用法示例
May 20 PHP
Yii2.0多文件上传实例说明
Jul 24 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 CLI模式下的多进程应用分析
2013/06/03 PHP
php中利用str_pad函数生成数字递增形式的产品编号
2013/09/30 PHP
php开发中的页面跳转方法总结
2015/04/26 PHP
浅谈php处理后端&amp;接口访问超时的解决方法
2016/10/29 PHP
php实现的mysqldb读写分离操作类示例
2017/02/07 PHP
Laravel框架实现的记录SQL日志功能示例
2018/06/19 PHP
一些有用的JavaScript和jQuery的片段分享
2011/08/23 Javascript
多种方式实现JS调用后台方法进行数据交互
2013/08/20 Javascript
jQuery实现友好的轮播图片特效
2015/01/12 Javascript
jquery遍历函数siblings()用法实例
2015/12/24 Javascript
JS实现简单的天数计算器完整实例
2017/04/28 Javascript
vue短信验证性能优化如何写入localstorage中
2018/04/25 Javascript
在vue中使用v-bind:class的选项卡方法
2018/09/27 Javascript
微信小程序Echarts覆盖正常组件问题解决
2019/07/13 Javascript
vue 验证码界面实现点击后标灰并设置div按钮不可点击状态
2019/10/28 Javascript
Vue快速实现通用表单验证功能
2019/12/05 Javascript
详解JavaScript 的执行机制
2020/09/18 Javascript
Python 除法小技巧
2008/09/06 Python
Python 多核并行计算的示例代码
2017/11/07 Python
Python中摘要算法MD5,SHA1简介及应用实例代码
2018/01/09 Python
Python数据类型中的“冒号“[::]——分片与步长操作示例
2018/01/24 Python
django组合搜索实现过程详解(附代码)
2019/08/06 Python
Python利用WMI实现ping命令的例子
2019/08/14 Python
Python Django 前后端分离 API的方法
2019/08/28 Python
pytorch自定义二值化网络层方式
2020/01/07 Python
印尼最大的婴儿用品购物网站:Orami
2017/09/28 全球购物
经济学人订阅:The Economist
2018/07/19 全球购物
亿企通软件测试面试题
2012/04/10 面试题
协商一致解除劳动合同协议书
2014/09/14 职场文书
工作检讨书怎么写
2015/01/23 职场文书
2015年世界卫生日活动总结
2015/02/09 职场文书
大学副班长竞选稿
2015/11/21 职场文书
高中历史教学反思
2016/02/19 职场文书
python实现ROA算子边缘检测算法
2021/04/05 Python
教你用Python爬取英雄联盟皮肤原画
2021/06/13 Python
html5调用摄像头实例代码
2021/06/28 HTML / CSS