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 相关文章推荐
discuz安全提问算法
Jun 06 PHP
php 方便水印和缩略图的图形类
May 21 PHP
php检测图片木马多进制编程实践
Apr 11 PHP
PHP字符串中特殊符号的过滤方法介绍
Feb 18 PHP
PHP实现自动对图片进行滚动显示的方法
Mar 12 PHP
PHP实现简单数字分页效果
Jul 26 PHP
Zend Framework框架路由机制代码分析
Mar 22 PHP
PHP实现统计在线人数功能示例
Oct 15 PHP
php使用Jpgraph创建折线图效果示例
Feb 15 PHP
php 中phar包的使用教程详解
Oct 26 PHP
php中如何执行linux命令详解
Nov 06 PHP
php基于 swoole 实现的异步处理任务功能示例
Aug 13 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
discuz authcode 经典php加密解密函数解析
2020/07/12 PHP
php存储过程调用实例代码
2013/02/03 PHP
Linux服务器下PHPMailer发送邮件失败的问题解决
2017/03/04 PHP
tp5 实现列表数据根据状态排序
2019/10/18 PHP
Div Select挡住的解决办法
2008/08/07 Javascript
关于javascript function对象那些迷惑分析
2011/10/24 Javascript
javascript学习笔记(八) js内置对象
2012/06/19 Javascript
jQuery Ajax()方法使用指南
2014/11/19 Javascript
jQuery使用之标记元素属性用法实例
2015/01/19 Javascript
jQuery模拟黑客帝国矩阵效果实例
2015/06/28 Javascript
js实现的星星评分功能函数
2015/12/09 Javascript
Express实现前端后端通信上传图片之存储数据库(mysql)傻瓜式教程(二)
2015/12/10 Javascript
jQuery实现进度条效果代码
2015/12/17 Javascript
jQuery与JavaScript节点创建方法的对比
2016/11/18 Javascript
使用vue制作探探滑动堆叠组件的实例代码
2018/03/07 Javascript
解决Layui选择全部,换页checkbox复选框重新勾选的问题方法
2018/08/14 Javascript
改变layer confirm弹窗按钮的颜色方法
2019/09/12 Javascript
vue使用recorder.js实现录音功能
2019/11/22 Javascript
小程序接入腾讯位置服务的详细流程
2020/03/03 Javascript
Vue文本模糊匹配功能如何实现
2020/07/30 Javascript
python实现通过shelve修改对象实例
2014/09/26 Python
python遍历序列enumerate函数浅析
2017/10/17 Python
使用pip发布Python程序的方法步骤
2018/10/11 Python
python实现图片横向和纵向拼接
2020/03/05 Python
opencv 图像礼帽和图像黑帽的实现
2020/07/07 Python
python自动生成证件号的方法示例
2021/01/14 Python
css3中less实现文字长阴影(long shadow)
2020/04/24 HTML / CSS
HTML块级标签汇总(小篇)
2016/07/13 HTML / CSS
AmazeUI 网格的实现示例
2020/08/13 HTML / CSS
美国室内盆栽植物购买网站:Plants.com
2020/04/24 全球购物
为什么说Ruby是一种真正的面向对象程序设计语言
2012/10/30 面试题
工程开工庆典邀请函
2014/02/01 职场文书
自我鉴定标准格式
2014/03/19 职场文书
毕业论文致谢部分怎么写
2015/05/14 职场文书
新娘婚礼答谢词
2015/09/29 职场文书
简单了解 MySQL 中相关的锁
2021/05/25 MySQL