简体   繁体   中英

PHP Simple HTML DOM Scrape External URL

I'm trying to build a personal project of mine, however I'm a bit stuck when using the Simple HTML DOM class.

What I'd like to do is scrape a website and retrieve all the content, and it's inner html, that matches a certain class.

My code so far is:

    <?php
    error_reporting(E_ALL);
    include_once("simple_html_dom.php");
    //use curl to get html content
    $url = 'http://www.peopleperhour.com/freelance-seo-jobs';

    $html = file_get_html($url);

    //Get all data inside the <div class="item-list">
    foreach($html->find('div[class=item-list]') as $div) {
    //get all div's inside "item-list"
    foreach($div->find('div') as $d) {
    //get the inner HTML
    $data = $d->outertext;
    }
    }
print_r($data)
    echo "END";
    ?>

All I get with this is a blank page with "END", nothing else outputted at all.

It seems your $data variable is being assigned a different value on each iteration. Try this instead:

$data = "";
foreach($html->find('div[class=item-list]') as $div) {
    //get all divs inside "item-list"
    foreach($div->find('div') as $d) {
         //get the inner HTML
         $data .= $d->outertext;
    }
}
print_r($data)

I hope that helps.

I think, you may want something like this

$url = 'http://www.peopleperhour.com/freelance-seo-jobs';
$html = file_get_html($url);
foreach ($html->find('div.item-list div.item') as $div) {
    echo $div . '<br />';
};

This will give you something like this (if you add the proper style sheet, it'll be displayed nicely)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM