1. [文件] HtmlDom.php 
<?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)
    {http://www.enterdesk.com/special/shouhui​
        if($this->_nodePath == '')
            $this->_nodePath = '//';
        else手绘图片
            $this->_nodePath .= '/';
  
        $nodes = $this->_xpath->query($this->_nodePath.$query);
        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);
    }
 
}

最近采集写的一个超简单实用的HTML解析类的更多相关文章

  1. 简单实用的PHP验证码类

    一个简单实用的php验证码类,分享出来 ,供大家参考. 代码如下: <?php /** @ php 验证码类 @ http://www.jbxue.com */ Class code { var ...

  2. 打造支持apk下载和html5缓存的 IIS(配合一个超简单的android APP使用)具体解释

    为什么要做这个看起来不靠谱的东西呢? 由于刚学android开发,还不能非常好的熟练控制android界面的编辑和操作,所以我的一个急着要的运用就改为html5版本号了,反正这个运用也是须要从serv ...

  3. 手写一个超简单的Vue

    基本结构 这里我根据自己的理解模仿了Vue的单文件写法,通过给Vue.createApp传入参数再挂载元素来实现页面与数据的互动. 其中理解不免有错,希望大佬轻喷. 收集数据 这里将Vue.creat ...

  4. 【小白学PyTorch】1 搭建一个超简单的网络

    文章目录: 目录 1 任务 2 实现思路 3 实现过程 3.1 引入必要库 3.2 创建训练集 3.3 搭建网络 3.4 设置优化器 3.5 训练网络 3.6 测试 1 任务 首先说下我们要搭建的网络 ...

  5. 一听就懂:用Python做一个超简单的小游戏

    写它会用到 while 循环random 模块if 语句输入输出函数

  6. DIY一个超简单的画图程序

    编译环境:VS2017+Easy_X 最近笔者一直在翻阅Easy_X的帮助手册,学习到了一些关于获取鼠标状态消息函数的知识,感觉收获颇大,于是想试验一番,将所学知识运用出来.先补充一下在Easy_X中 ...

  7. 【新手向】一个超简单的jquery.mCustomScrollbar滚动条插件Demo

    <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script> < ...

  8. 用CSS3写圆角(超简单)

    前缀: -moz(例如 -moz-border-radius)用于Firefox-webkit(例如:-webkit-border-radius)用于Safari和Chrome. CSS3圆角(所有的 ...

  9. gulp安装+一个超简单入门小demo

    gulp安装參考.gulp安装參考2. 一.NPM npm是node.js的包管理工具.主要功能是管理.更新.搜索.公布node的包. Gulp是通过npm安装的. 所以首先,须要安装node.js. ...

随机推荐

  1. jenkins构建一个go项目

    Jenkins安装   最低配置: 不少于256M内存,不低于1G磁盘,jdk版本>=8 安装jdk1.8 yum install -y java-1.8.0-openjdk wget -O / ...

  2. HTTP头解读

    Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET.POST.PUT.DELETE.一个URL地址用于描述一个网络上的资源, 而HTTP中的GET.POST.PUT. DELETE ...

  3. 2、C++ 的升级

    1.内联函数     define 可以定义宏代码片段,但是,C++ 推荐使用内联函数替代宏代码片段. inline int f(int a, int b) { }     只需要在 函数定义(实现) ...

  4. 开发ActiveX控件调用另一个ActiveX系列2——调试ActiveX

    关于调试ActiveX控件,有若干方法,例如可以建一个MFC工程调用调试,我则倾向于使用附加到浏览器进程,因为浏览器才是真正运行的环境. 打开加载ActiveX的目标页面,当然希望我们的调试内容不是自 ...

  5. php selenium 测试验证码问题

    $this->pause(10000)这段代码用于停止程序执行,可以在这个空隙内输入验证码

  6. 新手学习JSP+Servlet笔记一

    作为一个新手,初次接触jsp,servlet,习惯了后台的开发,前台的知识一窍不通,利用闲暇时间,给自己补补,从MyEclipse开始. 安装好MyEclipse之后,没有安装程序的可以下载 http ...

  7. Ubuntu 14.04lts安装vncserver

    之前有在centos上安装过非常多次vncserver,也写过一个centos 7上的安装文档.近来常识了好几次在ubuntu上安装都没有成功,这次最终搞定了.ubuntu自带的桌面是unity.这个 ...

  8. onvif 开发之video streamer---onvif实现功能和经验

    目录(?)[-] 一产生onvif源码框架 从wsdl生成C头文件 从头文件生成源码框架 二创建soap运行环境 三RTSP视频对接 实现GetCapabilities命令 实现GetServices ...

  9. wpf自定义colorpicker

    自定义colorpicker <ObjectDataProvider MethodName="GetType" ObjectType="{x:Type system ...

  10. mongodb 配置单实例与双实例

    环境: centos6.5 192.168.16.70 配置单实例mongodb:[root@www soft]# tar xf mongodb-linux-x86_64-rhel62-3.2.7.t ...