0

さて、私はhttp://simplehtmldom.sourceforge.net/にある PHP ベースの simple_html_dom.php を使用して Web ページをスクレイピングしています。ページ。最終的には、そのリストを使用して jsTree を初期化する予定ですが、ステップ 1 を通過できません。これには単純で簡単な解決策があることは知っていますが、それを理解できないようです。何時間もかけてウェブを検索し、最終的にここに投稿することにしました。

基本的に、これを変換したい:

<body>
    <div id='div0'>
        <span id='span0'> <img id='img1'> </span>
    </div>

    <div id='div1'>
        <span id='span1'>  </span>
    </div>
</body>

これに:

<ul>
    <li>
        div0
        <ul>
            <li>
                span0
                <ul>
                    <li>
                        img1
                    </li>
                </ul>
           </li>
        </ul>
    </li>
    <li>
        div1
        <ul>
            <li>
                span1
            </li>
        </ul>
    </li>
</ul>

私が正しいと思う例は次のようになりますが、次のように生成されます: `

<li><li>`Fatal error: Call to a member function children() on a non-object in main.php on line 46 

コード:

include_once('simple_html_dom.php');

$html = file_get_html("http://www.thefuckingweather.com/");

function create($url)
{
    print "<li>";
    $count = 0;

    foreach ($url as $chi)
    {
        if($chi->tag != "script")
        {
            if (count($chi->children()) > 0)  //#46
            {
                create($chi->children($count));
            }
            else
            {
                print "</li>";
            }
        }
        $count++;
    }   
}

create($html->find("body"));
4

1 に答える 1

0

理解した。私はとても疲れ果てていたので、これが起こったのかもしれません。答えはとてもシンプルでした。

include_once('simple_html_dom.php');

$html = file_get_html("http://www.reddit.com/");

foreach ($html->find("body") as $chi)
{
    test($chi);
}

$count = 0;

function test($t)
{
    print "<ul>";
    for ($i = 0; $i < count($t->children()); $i++)
    {
        print "<li>";
        print $t->children($i)->id . " - " . $t->children($i)->tag . $count++;
        test($t->children($i));
        print "</li>";
    }
    print "</ul>";
}
于 2011-03-27T21:51:15.690 に答える