Search code examples
phplistloopsweb-scrapingforeach

php how to scrape inside <li><a> using simplehtmldom?


HTML from site

    <ul id="blahlist">
    <li><a href="http://blahblah.com">blah blah</a></li>
    <li><a href="http://blahblah2.com">blah blah 2</a></li>
    ......
    </ul>

my code

$dom = new simple_html_dom();
$dom->load_file( "blah.html" );

    $div_category = $dom->find("#blahlist");

    foreach ($div_category as &$ul){
    $a_list = $ul->find("a");
    foreach ( $a_list as &$anchor){
        $csv_array=array($anchor->plaintext, $anchor->getAttribute("href") );
        fputcsv($csv_out, $csv_array);
        print_r($anchor);
    }

the problem is it only show the first row(first line) and not showing the rest of the list within blahlist. Am I doing something wrong? something to do with <li> that might have stopped after the first line?


Solution

  • How about

    $dom->find("#blahlist li");
    

    It is to "grab" all lis under #blahlist.