Search code examples
htmlweb-scrapingweb-crawlersimple-html-dom

Get all the <li> under particular <div>


I am using Curl to grab the URL and SIMPLE HTML DOM to get the data. I want the data which is under <li>. But the problem is there are other <ul> and <li>'s on the page. And they don't have classes or id's. Here is my html code.

<div class="parent" id="parent">
    <div class="child">
        <div class="grandchild">
            <p>Text Paragraph</p>
            <h2>Heading</h2>
            <ul>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
            </ul>
            <p>Text</p>
        </div>
    </div>
</div>

Only the top div has an id which is unique.


Solution

  • This will give you the result.

    $html = str_get_html('<div class="parent" id="parent">
    <div class="child">
        <div class="grandchild">
            <p>Text Paragraph</p>
            <h2>Heading</h2>
            <ul>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
            </ul>
            <p>Text</p>
        </div>
    </div>
    </div>');
    
    foreach($html->find('div#parent ul') as $ul) 
    {
       foreach($ul->find('li') as $li) 
       {
            echo $li->plaintext."<br>";
       }
    }