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的安全策略
Oct 09 PHP
sqlyog 中文乱码问题的设置方法
Oct 19 PHP
PHP stristr() 函数(不区分大小写的字符串查找)
Jun 03 PHP
php 数组使用详解 推荐
Jun 02 PHP
PHP用strstr()函数阻止垃圾评论(通过判断a标记)
Sep 28 PHP
phpmailer中文乱码问题的解决方法
Apr 22 PHP
在WordPress中使用wp_count_posts函数来统计文章数量
Jan 05 PHP
PHP解压ZIP文件到指定文件夹的方法
Nov 17 PHP
详解PHP使用日期时间处理器Carbon人性化显示时间
Aug 10 PHP
php 读取文件夹下所有图片、文件的实例
Oct 17 PHP
laravel添加角色和模糊搜索功能的实现代码
Jun 22 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
论建造顺序的重要性
2020/03/04 星际争霸
神族 PROTOSS 概述
2020/03/14 星际争霸
中国第一家无线电行
2021/03/01 无线电
PHP新手上路(七)
2006/10/09 PHP
thinkphp的静态缓存用法分析
2014/11/29 PHP
设置iframe的document.designMode后仅Firefox中其body.innerHTML为br
2012/02/27 Javascript
js indexOf()定义和用法
2012/10/21 Javascript
jQuery选择器全面总结
2014/01/06 Javascript
jQuery点缩略图弹出层显示大图片
2015/02/13 Javascript
JavaScript获取DOM元素的11种方法总结
2015/04/25 Javascript
jQuery实现的手风琴侧边菜单效果
2017/03/29 jQuery
微信小程序 商城开发(ecshop )简单实例
2017/04/07 Javascript
JavaScript实现的选择排序算法实例分析
2017/04/14 Javascript
jQuery+pjax简单示例汇总
2017/04/21 jQuery
Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条
2019/06/24 Javascript
解决Idea、WebStorm下使用Vue cli脚手架项目无法使用Webpack别名的问题
2019/10/11 Javascript
js实现简单抽奖功能
2020/11/24 Javascript
[38:30]2014 DOTA2国际邀请赛中国区预选赛 LGD-GAMING VS CIS 第一场2
2014/05/24 DOTA
Python 正则表达式(转义问题)
2014/12/15 Python
python+matplotlib演示电偶极子实例代码
2018/01/12 Python
python3+PyQt5实现自定义流体混合窗口部件
2018/04/24 Python
PyQt5 QTable插入图片并动态更新的实例
2019/06/18 Python
关于pymysql模块的使用以及代码详解
2019/09/01 Python
Python不支持 i ++ 语法的原因解析
2020/07/22 Python
python 线程的五个状态
2020/09/22 Python
Python解析m3u8拼接下载mp4视频文件的示例代码
2021/03/03 Python
使用css3绘制出各种几何图形
2016/08/17 HTML / CSS
css3实现一款模仿iphone样式的注册表单
2013/03/20 HTML / CSS
html5使用canvas实现弹幕功能示例
2017/09/11 HTML / CSS
浅谈html5与APP混合开发遇到的问题总结
2018/03/20 HTML / CSS
什么叫做SQL注入,如何防止
2016/10/04 面试题
中职生自我鉴定范文
2013/10/03 职场文书
初中考试作弊检讨书
2014/02/01 职场文书
用人单位终止解除劳动合同证明书
2014/10/06 职场文书
一定要知道的 25 个 Vue 技巧
2021/11/02 Vue.js
Mysql超详细讲解死锁问题的理解
2022/04/01 MySQL