0

I know there are similar question, but, trying to study PHP I met this error and I want understand why this occurs.

<?php
    $url = 'http://aice.anie.it/quotazione-lme-rame/';
    echo "hello!\r\n";
    $html = new DOMDocument();
    @$html->loadHTML($url);
    $xpath = new DOMXPath($html);
    $nodelist = $xpath->query(".//*[@id='table33']/tbody/tr[2]/td[3]/b");

    foreach ($nodelist as $n) {
        echo $n->nodeValue . "\n";
    }
?>

this prints just "hello!". I want to print the value extracted with the xpath, but the last echo doesn't do anything.

1
  • On the provided URL no id with table33 can be found.. Commented Jul 7, 2014 at 9:12

1 Answer 1

3

You have some errors in your code :

  1. You try to get the table from the url http://aice.anie.it/quotazione-lme-rame/, but it's actually in an iframe located at http://www.aiceweb.it/it/frame_rame.asp, so get the iframe url directly.

  2. You use the function loadHTML(), which load an HTML string. What you need is the loadHTMLFile function, which takes the link of an HTML document as a parameter (See http://www.php.net/manual/fr/domdocument.loadhtmlfile.php)

  3. You assume there is a tbody element on the page but there is no one. So remove that from your query filter.

Working code :

$url = 'http://www.aiceweb.it/it/frame_rame.asp';
echo "hello!\r\n";
$html = new DOMDocument();
@$html->loadHTMLFile($url);
$xpath = new DOMXPath($html);
$nodelist = $xpath->query(".//*[@id='table33']/tr[2]/td[3]/b");

foreach ($nodelist as $n) {
    echo $n->nodeValue . "\n";
}
Sign up to request clarification or add additional context in comments.

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.