duzhenxun
  • 2
  • 新人请关照

递归多级采集的一个问题

想把路径拼全,使用querylist如何拼呢??
`
<ul>

<li\><span class\="file"\>favicon.ico</span\></li\>  
<li class\="expandable"\>  
    <div class\="hitarea expandable-hitarea"\></div\>  
    <span class\="folder"\>banner-ads</span\>  
    <ul style\="display: block;"\>  
        <li\><span class\="img"\>ad01.png</span\></li\>  
        <li\><span class\="img"\>ad02.png</span\></li\>  
        <li\><span class\="img"\>ad03.png</span\></li\>  
        <li\><span class\="img"\>ad04.png</span\></li\>  
        <li\><span class\="img"\>ad06.jpg</span\></li\>  
    </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>
`

image.png

最终拼成如下
favicon.ico
banner-ads/ad01.png
news/category/category1.png
....
无限级的那种。看官方有个简单例子但有点区别。一直没有搞定。哪位大神指教一下??
管方类似例子:http://www.querylist.cc/docs/...

阅读 217
评论 更新于 2019-12-27
    1 个回答

    这样能实现你的要求,你的疑问关键在于如何拼接item

            $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);
    评论 赞赏 1月9日
      撰写回答

      登录后参与交流、获取后续更新提醒