PHP XPath查询什么都不返回

I've been recently playing with DOMXpath in PHP and had success with it, trying to get more experience with it I've been playing grabbing certain elements of different sites. I am having trouble getting the weather marker off of http://www.theweathernetwork.com/weather/cape0005 this website.

Specifically I want

//*[@id='theTemperature']

Here is what I have

$url = file_get_contents('http://www.theweathernetwork.com/weather/cape0005');

$dom   = new DOMDocument();
@$dom->loadHTML($url);
$xpath = new DOMXPath($dom);
$tags  = $xpath->query("//*[@id='theTemperature']");
foreach ($tags as $tag){
    echo $tag->nodeValue;
}

Is there something I am doing wrong here? I am able to produce actual results on other tags on the page but specifically not this one.

Thanks in advance.

what happens is straightforward, the page contains an empty id="theTemperature" element which is a placeholder to be populated with javascript. file_get_contents() will just download the page, not executing javascript, so the element remains empty. Try to load the page in the browser with javascript disabled to see it yourself

The element you're trying to select is indeed empty. The page loads the temperature into that id through ajax. Specifically this script:

http://www.theweathernetwork.com/common/js/master/citypage_ajax.js?cb=201301231338

but when you do a file_get_contents those scripts obviously don't get resolved. I'd go with guido's solution of using the RSS

You might want to improve your DOMDocument debugging skills, here some hints (Demo):

<?php
header('Content-Type: text/plain;');

$url = file_get_contents('http://www.theweathernetwork.com/weather/cape0005');

$dom   = new DOMDocument();
@$dom->loadHTML($url);
$xpath = new DOMXPath($dom);
$tags  = $xpath->query("//*[@id='theTemperature']");
foreach ($tags as $i => $tag){
    echo $i, ': ', var_dump($tag->nodeValue), ' HTML: ', $dom->saveHTML($tag), "
";
}
  1. Output the number of the found node, I do it here with $i in the foreach.
  2. var_dump the ->nodeValue, it helps to show what exactly it is.
  3. Output the HTML by making use of the saveHTML function which shows a better picture.

The actual output:

0: string(0) ""
 HTML: <p id="theTemperature"></p>

You can easily spot that the element is empty, so the temperature must go in from somewhere else, e.g. via javascript. Check the Network tools of your browser.