我敢肯定还有其他多种方法可以做到这一点,但我会这样做。
<?php
/**
* @author Bart Degryse
* @copyright 2013
*/
function getData() {
//Get the html page
$url = "http://www.essen-wetter.de/table.php";
$content = file_get_contents($url);
//Turn it into a dom document searchable by xpath
$dom = new DOMDocument();
$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
//Get field names
$query = "//tr/td[position()=1 and normalize-space(text()) = 'Zeit']";
$entries = $xpath->query($query);
$entry = $entries->item(0);
$tr = $entry->parentNode;
foreach ($tr->getElementsByTagName("td") as $td) {
$fieldnames[] = $td->textContent;
}
//Get field data
$query = "//tr/td[position()=1 and (substring-after(normalize-space(text()),':') = '00' or substring-after(normalize-space(text()),':') = '30')]";
$entries = $xpath->query($query);
foreach ($entries as $entry) {
$fieldvalues = array();
$tr = $entry->parentNode;
foreach ($tr->getElementsByTagName("td") as $td) {
$fieldvalues[] = $td->textContent;
}
$data[] = array_combine($fieldnames, $fieldvalues);
}
//Return data set
return $data;
}
//Gather the data
$data = getData();
//Do something with it
echo "<pre>\n";
foreach ($data as $row) {
echo "Temperature at {$row['Zeit']} was {$row['Temperatur']}.\n";
}
echo "</pre><hr><pre>\n";
print_r($data);
echo "</pre>\n";
?>
如果您要在 UTF-8 兼容终端或声明为 UTF-8 编码的网页上显示数据,则应该这样做。如果你想使用单字节 ISO-8859-1 编码但是你必须改变这一行:
$fieldnames[] = $td->textContent;
进入这个:
$fieldvalues[] = utf8_decode($td->textContent);
备注
请注意,虽然从技术上讲,这样做在法律上并不难,但您却处于松散的基础上。该页面上的数据受版权保护,归 Markus Wolter 所有。未经他的同意将他的数据用于您自己的目的被视为盗窃。