# 递归多级采集的一个问题

duzhenxun
• 2

`
<ul>

``````<li\><span class\="file"\>favicon.ico</span\></li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<ul style\="display: block;"\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>logos</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>logo-light.png</span\></li\>
<li\><span class\="img"\>logo.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>news</span\>
<ul style\="display: block;"\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>category</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>category1.png</span\></li\>
<li\><span class\="img"\>category2.png</span\></li\>
<li\><span class\="img"\>category3.png</span\></li\>
<li\><span class\="img"\>category4.png</span\></li\>
<li\><span class\="img"\>category5.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>fashion</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>image1.png</span\></li\>
<li\><span class\="img"\>image2.png</span\></li\>
<li\><span class\="img"\>image3.png</span\></li\>
<li\><span class\="img"\>image4.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>food</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>food01.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>health</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>image1.png</span\></li\>
<li\><span class\="img"\>image2.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>lifestyle</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>image1.jpg</span\></li\>
<li\><span class\="img"\>image2.png</span\></li\>
<li\><span class\="img"\>image3.png</span\></li\>
<li\><span class\="img"\>image4.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>news-details</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>large-image.jpg</span\></li\>
<li\><span class\="img"\>left-image.jpg</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>sports</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>sports02.png</span\></li\>
<li\><span class\="img"\>sports03.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>tech</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>image5.png</span\></li\>
<li\><span class\="img"\>tech02.png</span\></li\>
<li\><span class\="img"\>tech1.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>travel</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>image1.png</span\></li\>
<li\><span class\="img"\>image2.png</span\></li\>
<li\><span class\="img"\>image3.png</span\></li\>
</ul\>
</li\>
<li class\="expandable"\>
<div class\="hitarea expandable-hitarea"\></div\>
<span class\="folder"\>video</span\>
<ul style\="display: block;"\>
<li\><span class\="img"\>video1.jpg</span\></li\>
<li\><span class\="img"\>video2.jpg</span\></li\>
<li\><span class\="img"\>video3.jpg</span\></li\>
<li\><span class\="img"\>video4.jpg</span\></li\>
</ul\>
</li\>
<li\><span class\="img"\>author.png</span\></li\>
<li\><span class\="img"\>user1.png</span\></li\>
<li\><span class\="img"\>user2.png</span\></li\>
</ul\>
</li\>
<li\><span class\="img"\>controls.png</span\></li\>  ``````

</ul>
`

favicon.ico
news/category/category1.png
....

##### 1个回答

``````        \$html =<<<STR
<div id="demo">
<ul>
<li>
<h3>xxx</h3>
<div class="list">
<div class="item">item1</div>
<div class="item">item2</div>
</div>
</li>
<li>
<h3>xxx2</h3>
<div class="list">
<div class="item">item12</div>
<div class="item">item22</div>
</div>
</li>
</ul>
</div>
STR;

\$data = QueryList::html(\$html)->rules(array(
'title' => array('h3','text'),
'list' => array('.list','html')
))->range('#demo li')->queryData(function(\$item) {
// 注意这里的QueryList对象与上面的QueryList对象是同一个对象
// 所以这里要重置range()参数，否则会共用前面的range()参数，导致出现采集不到结果的诡异现象
\$item['list'] = QueryList::html(\$item['list'])->rules(array(
'item' => array('.item','text')
))->range('')->queryData();

// 拼接item ！！！
\$item['list'] = array_map(function(\$value) use( \$item )  {
\$value['item'] = \$item['title']. "/" .\$value['item'];
return \$value;
}, \$item['list']);

return \$item;
});
print_r(\$data);``````
##### 撰写回答
###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进