递归多级采集


可以在getData()方法中多次调用QueryList来实现递归多级采集。

示例

  1. <?php
  2. require 'QueryList/vendor/autoload.php';
  3. use QL\QueryList;
  4. //获取每个li里面的h3标签内容,和class为item的元素内容
  5. $html =<<<STR
  6. <div id="demo">
  7. <ul>
  8. <li>
  9. <h3>xxx</h3>
  10. <div class="list">
  11. <div class="item">item1</div>
  12. <div class="item">item2</div>
  13. </div>
  14. </li>
  15. <li>
  16. <h3>xxx2</h3>
  17. <div class="list">
  18. <div class="item">item12</div>
  19. <div class="item">item22</div>
  20. </div>
  21. </li>
  22. </ul>
  23. </div>
  24. STR;
  25. $data = QueryList::Query($html,array(
  26. 'title' => array('h3','text'),
  27. 'list' => array('.list','html')
  28. ),'#demo li')->getData(function($item){
  29. $item['list'] = QueryList::Query($item['list'],array(
  30. 'item' => array('.item','text')
  31. ))->data;
  32. return $item;
  33. });
  34. print_r($data);
  35. /**
  36. 结果:
  37. Array
  38. (
  39. [0] => Array
  40. (
  41. [title] => xxx
  42. [list] => Array
  43. (
  44. [0] => Array
  45. (
  46. [item] => item1
  47. )
  48. [1] => Array
  49. (
  50. [item] => item2
  51. )
  52. )
  53. )
  54. [1] => Array
  55. (
  56. [title] => xxx2
  57. [list] => Array
  58. (
  59. [0] => Array
  60. (
  61. [item] => item12
  62. )
  63. [1] => Array
  64. (
  65. [item] => item22
  66. )
  67. )
  68. )
  69. )
  70. */