0

0

最近采集写的一个超简单实用的HTML解析类

php中文网

php中文网

发布时间:2016-07-25 09:02:04

|

932人浏览过

|

来源于php中文网

原创

$xp = new xf_HtmlDom(); $xp->loadHtml('http://dealer.bitauto.com/100040078/cars.html'); $rows = $xp->find('dl/dd/a', 0)->innertext; print_r($rows);
  1. $oldSetting = libxml_use_internal_errors( true );
  2. libxml_clear_errors();
  3. /**
  4. *
  5. * -+-----------------------------------
  6. * |PHP5 Framework - 2011
  7. * |Web Site: www.iblue.cc
  8. * |E-mail: mejinke@gmail.com
  9. * |Date: 2012-10-12
  10. * -+-----------------------------------
  11. *
  12. * @desc HTML解析器
  13. * @author jingke
  14. */
  15. class XF_HtmlDom
  16. {
  17. private $_xpath = null;
  18. private $_nodePath = '';
  19. public function __construct($xpath = null, $nodePath = '')
  20. {
  21. $this->_xpath = $xpath;
  22. $this->_nodePath = $nodePath;
  23. }
  24. public function loadHtml($url)
  25. {
  26. 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');
  27. $content = '';
  28. if(strpos(strtolower($url), 'http')===false)
  29. {
  30. $content = file_get_contents($url);
  31. }
  32. else
  33. {
  34. $ch = curl_init();
  35. $user_agent = "Baiduspider+(+http://www.baidu.com/search/spider.htm)";
  36. $user_agent1='Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0';
  37. curl_setopt($ch, CURLOPT_URL, $url);
  38. curl_setopt($ch, CURLOPT_HEADER, false);
  39. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  40. curl_setopt($ch, CURLOPT_REFERER, $url);
  41. curl_setopt($ch, CURLOPT_USERAGENT, $user_agent1);
  42. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
  43. $content =curl_exec($ch);
  44. curl_close($ch);
  45. }
  46. $html = new DOMDocument();
  47. $html->loadHtml($content);
  48. $this->_xpath = new DOMXPath( $html );
  49. return $this;
  50. }
  51. public function find($query, $index = null)
  52. {
  53. if($this->_nodePath == '')
  54. $this->_nodePath = '//';
  55. else
  56. $this->_nodePath .= '/';
  57. $nodes = $this->_xpath->query($this->_nodePath.$query);
  58. if ($index == null && !is_numeric($index))
  59. {
  60. $tmp = array();
  61. foreach ($nodes as $node)
  62. {
  63. $tmp[] = new XF_HtmlDom($this->_xpath, $node->getNodePath());
  64. }
  65. return $tmp;
  66. }
  67. return new XF_HtmlDom($this->_xpath,$this->_xpath->query($this->_nodePath.$query)->item($index)->getNodePath());
  68. }
  69. /**
  70. * 获取内容
  71. */
  72. public function text()
  73. {
  74. if ($this->_nodePath != '' && $this->_xpath != null )
  75. return $this->_xpath->query($this->_nodePath)->item(0)->textContent;
  76. else
  77. return false;
  78. }
  79. /**
  80. * 获取属性值
  81. */
  82. public function getAttribute($name)
  83. {
  84. if ($this->_nodePath != '' && $this->_xpath != null )
  85. return $this->_xpath->query($this->_nodePath)->item(0)->getAttribute($name);
  86. else
  87. return false;
  88. }
  89. public function __get($name)
  90. {
  91. if($name == 'innertext')
  92. return $this->text();
  93. else
  94. return $this->getAttribute($name);
  95. }
  96. }
复制代码


相关文章

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
c++主流开发框架汇总
c++主流开发框架汇总

本专题整合了c++开发框架推荐,阅读专题下面的文章了解更多详细内容。

97

2026.01.09

c++框架学习教程汇总
c++框架学习教程汇总

本专题整合了c++框架学习教程汇总,阅读专题下面的文章了解更多详细内容。

51

2026.01.09

学python好用的网站推荐
学python好用的网站推荐

本专题整合了python学习教程汇总,阅读专题下面的文章了解更多详细内容。

139

2026.01.09

学python网站汇总
学python网站汇总

本专题整合了学python网站汇总,阅读专题下面的文章了解更多详细内容。

12

2026.01.09

python学习网站
python学习网站

本专题整合了python学习相关推荐汇总,阅读专题下面的文章了解更多详细内容。

19

2026.01.09

俄罗斯手机浏览器地址汇总
俄罗斯手机浏览器地址汇总

汇总俄罗斯Yandex手机浏览器官方网址入口,涵盖国际版与俄语版,适配移动端访问,一键直达搜索、地图、新闻等核心服务。

83

2026.01.09

漫蛙稳定版地址大全
漫蛙稳定版地址大全

漫蛙稳定版地址大全汇总最新可用入口,包含漫蛙manwa漫画防走失官网链接,确保用户随时畅读海量正版漫画资源,建议收藏备用,避免因域名变动无法访问。

432

2026.01.09

php学习网站大全
php学习网站大全

精选多个优质PHP入门学习网站,涵盖教程、实战与文档,适合零基础到进阶开发者,助你高效掌握PHP编程。

49

2026.01.09

php网站搭建教程大全
php网站搭建教程大全

本合集专为零基础用户打造,涵盖PHP网站搭建全流程,从环境配置到实战开发,免费、易懂、系统化,助你快速入门建站!

13

2026.01.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
php初学者入门课程
php初学者入门课程

共10课时 | 0.6万人学习

第六期_前端基础
第六期_前端基础

共107课时 | 18.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号