0

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.

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately this still wouldn't work. However, when defining the class like Sheikh's answer, it works perfectly.
0

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)

enter image description here

2 Comments

Perfect! Works as expected. How come it wouldn't work when defining the class like: div[class=item-list]?
You probably needed quotes around item-list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.