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. Shell脚本之:if-else

    Shell 有三种 if ... else 语句: 1.if ... fi 语句: 2.if ... else ... fi 语句: 3.if ... elif ... else ... fi 语句. ...

  2. HttpWebRequest.Proxy属性

    HttpWebRequest.Proxy默认使用IE代理,可以设置“Internet选项-Internet属性-连接-局域网设置-代理服务器”.

  3. zoj 2949 - Coins of Luck

    题目:有2中面条各n碗.每次抛硬币推断吃哪一种(到一种吃完为止).问抛硬币的数学期望. 分析:动态规划.概率dp.求出每种结束状态(即,有一种吃完)的概率,分别乘以步长即为期望. 大黄解法:状态位剩余 ...

  4. ubuntu16.04下Cmake学习一

    根据网上的资料,我总结了一下,一个工程应该有根目录(bin)存放可执行文件,头文件目录(include)存放头文件,源码文件(src)存放你的算法,还需要一个库文件夹存放你编译的静态库或者动态库.然后 ...

  5. nginx支持pathinfo模式

    很久不使用apache了,渐渐对apache感到陌生,因为朋友有个ZendFramework框架从apache移到nginx下,需要pathinfo模式支持.网上海搜于是开始搜索nginx+pathi ...

  6. Centos 7.0防火墙问题

    从Centos7开始,自带的防火墙从iptables更改成了firewall.一般在企业环境,出于人力和稳定性考虑,还是用成熟的技术比较稳妥. 以下是关闭firewall的方法 systemctl s ...

  7. 【Python】用Python打开IE、谷歌等浏览器报错及解决办法

    以IE浏览器为例: 当Python Shell输入下面代码时: >>> # coding=utf-8 >>> from selenium import webdri ...

  8. chessy 提高篇系列 阅读笔记

    java提高篇(一)—–理解java的三大特性之封装 封装的好处, 汇聚属性和方法 减少修改对 其他处的影响 控制get和set方法. java提高篇(二)—–理解java的三大特性之继承 继承的好处 ...

  9. 01 redis特点及安装使用

    一:redis的特点 ()redis是一个开源,BSD许可高级的key-value存储系统.可以用来存储字符串,哈希结构,链表,集合,因此,常用来提供数据结构服务. 二:redis和memcached ...

  10. Android UI开发第四十三篇——使用Property Animation实现墨迹天气3.0引导界面及动画实现

    前面写过<墨迹天气3.0引导界面及动画实现>,里面完美实现了动画效果,那一篇文章使用的View Animation,这一篇文章使用的Property Animation实现.Propert ...