LikeNoOther
LikeNoOther

Reputation: 11

How to download a specific DIV from a site using cURL?

I'm learning PHP and I wanted to know how to download a specific DIV every one or two hour automatically via cURL from a website.

Upvotes: 0

Views: 2031

Answers (3)

PiZzL3
PiZzL3

Reputation: 2210

UNTESTED (may be errors):

set_time_limit(3600*24); //24 hours

$numDownloads = 12; 

for ($i = 0; $i < $numDownloads; $i++) 
{
    $ch = curl_init('http://www.example.com/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);

    if (preg_match('/<div>(.*?)<\/div>/i', $content, $matches)) 
    {
        $divContents = $matches[1];

        $myFile = 'div'.$i.'.txt'; 

        if ($fh = fopen($myFile, 'w')) 
        {
            fwrite($fh, $divContents) 
        } 

        fclose($fh);
    }


    sleep(3600*2); 
}

I suggest you drop the loop and run that on a cron job though....

Upvotes: 1

Jon Gauthier
Jon Gauthier

Reputation: 25572

You can download the page with cURL, and parse it with SimpleXML to find what you need.

SimpleXMLElement::xpath might be the fastest way to find what you are looking for.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798516

You don't. You download the whole page, then parse it for the specific DIV you're interested in.

Upvotes: 2

Related Questions