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);
    }
 
}

最新文章

  1. jQuery Mobile案例,最近用Moon.Web和Moon.Orm做了一套系统
  2. PHP魔法方法的使用
  3. Python中is和==的区别
  4. Char、AnsiChar、WideChar、PChar、PAnsiChar、PWideChar 的用法
  5. elasticsearch 跨网段组集群
  6. 309. Best Time to Buy and Sell Stock with Cooldown
  7. Intent (一)
  8. ajax传递的数据类型json传递
  9. replication across two data centers
  10. Xamarin.Android 入门之:Xamarin+vs2015 环境搭建
  11. asp.net中使用Global.asax文件中添加应用出错代码,写入系统日志文件或数据库
  12. java里程碑之泛型--类型通配符
  13. django xadmin 集成DjangoUeditor富文本编辑器
  14. [Swift]LeetCode212. 单词搜索 II | Word Search II
  15. luogu P2303 [SDOi2012]Longge的问题
  16. 大佬带你深入浅出Lua虚拟机
  17. linux安装oracle 报错[INS-20802] Oracle Net Configuration Assistant failed 解决办法
  18. 蓝色简洁的企业cms网站权限后台管理模板——后台
  19. Oracle扩容表空间
  20. Python内置函数详解-总结篇

热门文章

  1. java8新特性学习笔记(二) 使用流(各种API)
  2. vue v-model使用说明
  3. hdu 4372 第一类斯特林数
  4. Oracle 修改字段注释
  5. HTML/CSS开发规范指南
  6. 转:PCIe基础知识
  7. Angular+Angular-Ui实现分页(代码更加简单,更加容易懂哦)
  8. Canvas中图片翻转的应用
  9. 一个数组nums,其中任意两个值等于给定值target,返回这两个值在nums里的位置
  10. 16 redis之sentinel运维监控