<?php
function getweather($city){
$url="http://www.weather.com.cn/weather1d/".$city.".shtml";
$result=file_get_contents($url);
$arr=explode("<li class='dn on' data-dn='todayT'>",$result);
$arr1=explode("<div class=\"m m2\" id=\"2_3d\">",$arr[1]);
$wt= mb_convert_encoding($arr1[0],"GBK","UTF-8");
$wt1=explode("</i> </p>",$wt);
$first=explode("</h1>",$wt1[0]);
$w1_1=$first[0];
//echo strip_tags($w1_1);//当前状态(第一个天气情况)
$w1_2=explode("<p class=\"tem\">",$first[1]);
//echo strip_tags($w1_2[0]);//天气
//echo strip_tags($w1_2[1]);//温度
$sec=explode("<h1>",$wt1[1]);
$second=explode("</h1>",$sec[1]);
$w2_1=$second[0];
//echo strip_tags($w2_1);//当前状态(第二个天气情况)
$w2_2=explode("<p class=\"tem\">",$second[1]);
//echo strip_tags($w2_2[0]);//天气
//echo strip_tags($w2_2[1]);//温度
$weekday=array('天','一','二','三','四','五','六');
$showtime=date("d号 星期").$weekday[date("w")];
//first温度
$firstwd=explode("°", trim(strip_tags($w1_2[1])));
$str="[今天] ".$showtime.strip_tags($w1_1)."至".strip_tags($w2_1)." ".trim(strip_tags($w1_2[0]))."转".trim(strip_tags($w2_2[0]))." ".$firstwd[0]."/".trim(strip_tags($w2_2[1]));
echo $str;
}
getweather(101120501);
?>php 抓取天气情况 www.weather.com.cn,布布扣,bubuko.com
原文:http://blog.csdn.net/zxlstudio/article/details/38676861