【问题标题】:PhP curl simple_dom_document request to get snow data from snowbird.comPhP curl simple_dom_document 请求从 snowbird.com 获取雪数据
【发布时间】:2016-02-14 14:16:37
【问题描述】:

我正在使用 php、curl 和 simple_dom_document 从 snowbird.com 获取雪数据。问题是我似乎无法真正找到我需要的数据。我能够找到父 div 及其名称,但我找不到实际的雪信息 div。这是我的代码。在我的代码下面有一小部分输出。

<?php
require('simple_html_dom.php');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.snowbird.com/mountain-report/"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT     5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$content = curl_exec($ch);
curl_close($ch);


$html = new simple_html_dom(); 
$html->load($content);


$ret = $html->find('.horizSnowChartText');
$ret = serialize($ret);
$ret3 = new simple_html_dom();
$ret3->load($ret);
$es = $ret3->find('text');
$ret2 = $ret3->find('.total-inches');

print_r($ret2);
//print_r($es);
?>

这是输出的图片。你可以看到它跳过了实际的雪数据并直接进入英寸标记“。

【问题讨论】:

  • 如果你已经找到了,一个简单的echo 就可以了
  • 您只是想要总英寸数? echo $ret3-&gt;find('.total-inches')-&gt;innertext;
  • @Ohgodwhy 非常感谢你,我发现我的问题是没有使用 ->innertext,就像你在评论中显示的那样。

标签: php html curl simple-html-dom


【解决方案1】:

请注意,您获得的 html 标记有多个 .total-inches 实例(具有此类的多个节点)。如果要显式获取一个,可以直接使用-&gt;find()的第二个参数指向它。

例子:

$ret2 = $html->find('.total-inches', 3);
                                   // ^

如果您想全部检查,一个简单的foreach 就足够了:

foreach($html->find('.current-conditions .snowfall-total .total-inches') as $in) {
    echo $in , "\n";
}

【讨论】:

  • 非常感谢幽灵。在你和 Ohgodwhys 的帮助下,我得以成功。你们是救生员
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 2016-05-14
相关资源
最近更新 更多