0

试图从这种糟糕的 HTML 格式中获取数据

http://www.weather.gov.sg/lws/zoneInfo.do

我只需要获取 3 个地方的数据,例如勿洛、城市和加东。为此,如何将数据存储在数组中?

这就是我为获得商店的前 5 行所做的,这并不是我想要的。

$row_counter='0';
while($row_counter<5)
{
$ret['Name'][] = $html->find('.FORM1', $row_counter)->innertext;
$ret['Area'][] = $html->find('.FORM1', $row_counter)->next_sibling()->innertext;
$ret['Alert'][] = $html->find('.FORM1', $row_counter)->next_sibling()->next_sibling()->innertext;
$ret['From'][] = $html->find('.FORM1', $row_counter)->next_sibling()->next_sibling()->next_sibling()->innertext;
$ret['Till'][] = $html->find('.FORM1', $row_counter)->next_sibling()->next_sibling()->next_sibling()->next_sibling()->innertext;
$row_counter++;
}

我能够成功地为整行和所有列存储数据。搜索某个名称的最有效方法是什么,例如 Bedok 并像 next_sibling 一样获取它旁边的列?

谢谢。

4

1 回答 1

3

是不是很容易。先试一试再问。(:

<?php
include 'simple_html_dom.php';
$html = file_get_html('http://www.weather.gov.sg/lws/zoneInfo.do');

$n = 0;
$table = $html->find('table',3)->find('table',0)->find('table',0)->find('table',0)->find('table',3)->find('table',0);

$i = -3;
$rows = $table->find('tr');
$holder = array();

foreach($rows as $element){
    $i++;
    if($i < 0) continue;

    $holder[$i]['name'] = $element->find('td',0)->plaintext;
    $holder[$i]['zone_or_school'] = $element->find('td',1)->plaintext;
    $holder[$i]['risk'] = $element->find('td',2)->plaintext;
    $holder[$i]['from'] = $element->find('td',3)->plaintext;
    $holder[$i]['till'] = $element->find('td',4)->plaintext;
}

var_dump($holder);
?>

如果您想获取特定数据,则可以将其过滤掉:

foreach($holder as $key => $val)
{
if($holder[$key]['name']=='Bedoc')
$my_data = $holder[$key];
}

此代码未调试,因为我现在在移动设备上。但也许你已经明白了,如果不工作的话。谢谢

于 2013-04-01T11:34:13.237 回答