A simple and practical HTML parsing class recently collected and written-php Tutorial

Source: Internet
Author: User
A simple and practical HTML parsing class recently collected and written
$ Xp = new xf_HtmlDom (); $ xp-> loadHtml ('http: // dealer.bitauto.com/100040078/cars.html'); $ rows = $ xp-> find ('dl/dd/', 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/123456 ';
  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 attribute 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. }

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.