Querylist匹配多个结果的问题

直接上测试代码,相同选择器,遇见多个匹配,返回结果没有被分配到一个数组键里,而是分开的。

html:

<div class="div1">1a</div>
<div class="div2">
    <span class="span">2a</span>
    <span class="span">2b</span>
    <span class="span">2c</span>
</div>

php:

$rules = [
    'div1' => ['.div1', 'text'],
    'div2' => ['.div2 .span', 'text'],
];

$data = QueryList::Query($link, $rules)->getData();
print_r($data);

错误result:

[
    0 => [
        'div1' => '1a',
        'div2' => '2a',
    ],
    1 => [
        'div2' => '2b',
    ],
    2 => [
        'div2' => '2c',
    ],
]

期望result:

[
    0 => [
        'div1' => '1a',
        'div2' => [
            '2a', '2b', '2c',
        ],
    ],
]
阅读 1.3k
评论
    1 个回答
    • 451

    这种情况需要用到递归多级采集,查看文档:http://v3.querylist.cc/site/index/doc/25

    例子:

    //获取每个li里面的h3标签内容,和class为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::Query($html,array(
            'title' => array('h3','text'),
            'list' => array('.list','html')
        ),'#demo li')->getData(function($item){
        $item['list'] = QueryList::Query($item['list'],array(
                 'item' => array('.item','text')
            ))->data;
        return $item;
    });
    print_r($data);
      撰写回答

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