我正处于Rob Percival的完整网络开发课程中,并坚持挑战Project:Weather Scraper。试图从网站获取天气信息,但无法正常工作。提前致谢
<?php
$contents = file_get_contents("http://www.weather-forecast.com/locations/San-Francisco/forecasts/latest");
preg_match('/3 Day Weather Forecast Summary:<\/b><span class="read-more-small"><span class="read-more-content"> <span class="phrase"> (.*?) </s', $contents, $matches);
echo $matches[1];
?>
答案 0 :(得分:0)
<span class="phrase">
之后的html中没有空格。
来自网站的HTML:
3 Day Weather Forecast Summary:</b><span class="read-more-small"><span class="read-more-content"> <span class="phrase">Light rain (total 5mm)
试试这个:
if (preg_match('/3 Day Weather Forecast Summary:<\/b><span class="read-more-small"><span class="read-more-content"> <span class="phrase">(.*?)</s', $contents, $matches)) {
echo $matches[1];
}
答案 1 :(得分:0)
我正在采取相同的课程,最后在一小时后想出如何做到这一点!对不起,如果这已经很晚了,但我希望能帮助其他人寻找答案。
我不确定你为什么不工作,但这对我有用:
preg_match('/3 Day Weather Forecast Summary:<\/b><span class="read-more-small"><span class="read-more-content"> <span class="phrase">(.*?)<\/span>/',
$contents, $matches);
print_r($matches[1]);
答案 2 :(得分:-1)
你可以试试这个:
3 Day Weather Forecast Summary:<\/b>\s*<span class="read-more-small">\s*<span class="read-more-content">\s*<span class="phrase">(.*?)<\/span>
它将处理任何标签之间的空白。