【问题标题】:Display external html specific value显示外部 html 特定值
【发布时间】:2018-03-29 06:28:10
【问题描述】:

我想从这个 html 页面显示“hiOutsideTemp”值:http://amira.meteokrites.gr/ZWNTANA.htm 到我的页面。这是一个临时值。

我正在使用以下代码:

<?php

require_once 'simple_html_dom.php';
$html_string = file_get_contents('http://amira.meteokrites.gr/ZWNTANA.htm');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$values = array();
$row = $xpath->query('//td[@id="/html/body/table/tbody/tr[7]/td[2]"]');
foreach($row as $value) {
    $values[] = trim($value->textContent);
}

echo '<pre>';
print_r($values);
?>

但我没有得到任何结果。 我该怎么办?

【问题讨论】:

  • 我会研究类似 PHPQuery 的东西,而不是 Dom 和 Xpath。但这只是我的偏好。基本上,它允许您使用 Jquery 之类的选择器。 $Doc-&gt;find('.hiOutsideTemp')-&gt;text()
  • 你不能只使用 HTML iframe 吗?

标签: php html parsing file-get-contents


【解决方案1】:

查看您拥有的 URL 的内容,它似乎只是一个设置列表,而不是 HTML 页面(仅当您没有包含实际的 HTML 文件时才会显示)。内容类似于...

imerominia="29/03/18";
ora=" 9:37";
sunriseTime=" 7:10";
sunsetTime="19:37";
ForecastStr=" Mostly cloudy and cooler.  Windy with possible wind shift to the W, NW, or N. ";
tempUnit="°C";
outsideTemp="9.3";
hiOutsideTemp="9.5";
hiOutsideTempAT=" 0:03";
lowOutsideTemp="6.2";
lowOutsideTempAT=" 4:24";
...

因此,您可以像加载 ini 文件格式一样加载它,这会为您提供数据的关联数组。

$html_string = file_get_contents('http://amira.meteokrites.gr/ZWNTANA.htm');
$data = parse_ini_string($html_string);

echo $data["hiOutsideTemp"];  // outputs - 9.5

【讨论】:

  • 谢谢!这样就可以了!
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 2015-03-20
  • 2013-07-21
  • 2018-01-24
  • 2013-12-31
  • 2019-09-24
  • 2012-11-11
  • 1970-01-01
相关资源
最近更新 更多