如何使用file_get_HTML查找HTML元素


How to find HTML element using file_get_html

使用库simple_html_dom.php

$html = file_get_html($link);

在类似的结构中

<div class="ps">    
    <h3>Lorem ipsum 1</h3>
    <p>Lorem ipsum 2</p>    
    <h3>Lorem ipsum 3</h3>
    <p>Lorem ipsum 4</p>
    <div class="extras250">
        <div class="boxType3 naSkroty">
            <div class="boxBody shortList">
                <h3>Lorem ipsum 5</h3>                
            </div>
        </div>
        <div class="boxType4 wsparcie">
            <div class="boxBody">
                <h3>Lorem ipsum 6</h3>
                <p>Lorem ipsum 7</p>                
            </div>
        </div>
    </div>
</div>

foreach ($html->find('.ps h3') as $naglowek) {
     $info['naglowek'][$i] = $naglowek->plaintext;
     $i++;
     }

我想找到<h3>,但只是第一级(不嵌套),但foreach可以找到所有这些。如何做到这一点?我试过

foreach ($html->find('.ps > h3') as $naglowek)

但不起作用。

不确定,但检查一次

foreach ($html->find('.ps > h3:first') as $naglowek)