Thursday 8 August 2013

How to scrape html contents of one div by id using php

How to scrape html contents of one div by id using php

The page on another domain which I'd like to scrape one div from contains:
<div id="thisone">
<p>Stuff</p>
</div>
<div id=="notthisone">
<p>More stuff</p>
</div>
Using this php...
<?php
$page = file_get_contents('http://thisite.org/source.html');
$doc = new DOMDocument();
$doc->loadHTML($page);
foreach ($doc->getElementsByTagName('div') as $node) {
echo $doc->saveHtml($node), PHP_EOL;
}
?>
...gives me all divs on http://thisite.org/source.html, with html.
However, I only want to pull through the div with an id of "thisone" but
using:
foreach ($doc->getElementById('thisone') as $node) {
doesn't bring up anything.

No comments:

Post a Comment