3

The page on another of my domains 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.

3 Answers 3

4
$doc->getElementById('thisone');// returns a single element with id this one

Try $node=$doc->getElementById('thisone'); and then print $node

On a side note, you can use phpQuery for a jquery like syntext: pq("#thisone")

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

Comments

1

$doc->getElementById('thisone') returns a single DOMElement, not an array, so you can't iterate through it

just do:

$node = $doc->getElementById('thisone');
echo $doc->saveHtml($node), PHP_EOL;

Comments

1

Look at PHP manual http://php.net/manual/en/domdocument.getelementbyid.php getElementByID returns an element or NULL. Not an array and therefore you can't iterate over it.

Instead do this

<?php
    $page = file_get_contents('example.html');
    $doc = new DOMDocument();
    $doc->loadHTML($page);
    $node = $doc->getElementById('thisone');
     echo $doc->saveHtml($node), PHP_EOL;
?>

On running php edit.php you get something like this

<div id="thisone">
      <p>Stuff</p>
  </div>

Comments

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.