문제

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.

도움이 되었습니까?

해결책 2

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>";
   }
}

다른 팁

You can retrieve the li items with:

foreach ($html->find("#parent li") as $li)
    echo $li->plaintext . "\n";

There is no need for double iteration over the ULs and then the LIs.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top