How to imitate child selector with Simple HTML DOM?

对着背影说爱祢 提交于 2019-12-02 03:46:23

问题


Fellas!

I have one nasty page to parse but can't figure out how to extract correct data blocks from it using Simple HTML DOM, because it has no CSS child selector support.

HTML:

<ul class="ul-block">
   <li>xxx</li>
   <li>xxx</li>
   <li>
      <ul>
         <li>xxx2</li>
      </ul>
</ul>

How would I extract (direct) child li elements of parent ul.ul-block?

The $node->find('ul[class=ul-block] > li'); doesn't work and $node->find('ul[class=ul-block] li'); ofc finds also nested descandant li elements :(


回答1:


Simple example with php DOM:

$dom = new DomDocument;
$dom->loadHtml('
<ul class="ul-block">
   <li>a</li>
   <li>b</li>
   <li>
      <ul>
         <li>c</li>
      </ul>
   </li>
</ul>
');

$xpath = new DomXpath($dom);
foreach ($xpath->query('//ul[@class="ul-block"]/li') as $liNode) {
    echo $liNode->nodeValue, '<br />';
}



回答2:


I had the same issue, and used the children method to grab just the first level items.

<ul class="my-list">
    <li>
        <a href="#">Some Text</a>
        <ul>
            <li><a href="#">Some Inner Text</a></li>
            <li><a href="#">Some Inner Text</a></li>
            <li><a href="#">Some Inner Text</a></li>
            <li><a href="#">Some Inner Text</a></li>
        </ul>
    </li>
    <li>
        <a href="#">Some Text</a>
        <ul>
            <li><a href="#">Some Inner Text</a></li>
            <li><a href="#">Some Inner Text</a></li>
            <li><a href="#">Some Inner Text</a></li>
            <li><a href="#">Some Inner Text</a></li>
        </ul>
    </li>
</ul>

And here's the Simple HTML Dom code to get just the first level li items:

$html = file_get_html( $url );
$first_level_items = $html->find( '.my-list', 0)->children();

foreach ( $first_level_items as $item ) {
    ... do stuff ...
}


来源:https://stackoverflow.com/questions/6831597/how-to-imitate-child-selector-with-simple-html-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!