A super-simple and useful HTML parsing class for recent acquisitions and writes

Source: Internet
Author: User
$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 Parser
  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. * Get content
  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. * Get Property values
  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. }
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.