【发布时间】:2016-03-25 08:41:08
【问题描述】:
我是编程新手。所以我选择使用 Wordpress 构建网页。但是我正在尝试从其他站点收集天气数据,我找不到合适的插件来抓取数据,因此决定尝试一下并自己整理一些东西。 但是由于我对编程的有限理解给了我一些问题。从网络上得到一点灵感,我把它放在一起:
$html = file_get_contents('http://www.frederikshavnhavn.dk/scripts/weatherwindow.php?langid=2'); //get the html returned from the following url
$poke_doc = new DOMDocument();
libxml_use_internal_errors(false); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$poke_doc->loadHTML($html);
libxml_clear_errors(); //remove errors for yucky html
$poke_xpath = new DOMXPath($poke_doc);
//get all the spans's with an id
$poke_type = $poke_xpath->query("//span[@class='weathstattype']");
$poke_text = $poke_xpath->query("//span[@class='weathstattext']");
foreach($poke_text as $text){
foreach($poke_type as $type){
echo $type->nodeValue;
echo $text->nodeValue . "</br>";
continue 2;
}
break;
}
}
这对我来说是全新的,我真的很想让它为我工作,希望能更好地理解作品背后的代码。
我想要实现的是带有数据的格式化列表。 1. 价值 $type $text 2. 价值 $type $text
现在它给我带来了很多麻烦。 当我使用 continue 2 时,它不会更改值 $type,但是当我只使用 continue 语句时,它会更改 $type 但不会更改 $text。我怎样才能让它每次都改变这两个值?
感谢您的帮助。
【问题讨论】:
-
您应该添加
var_dump($poke_type);和var_dump($poke_text);的输出。这些至少包含您需要的所有信息吗? -
是的,他们应该提供所有信息。但我不确定为什么需要 var_dump($poke_type);和 var_dump($poke_text); ?或者放在哪里,因为我看到他们只给了我几个错误?
-
您可能想试试Goutte。这是你如何使用它stackoverflow.com/questions/15628926/…
标签: php