php 從http://www.weather.com.cn/上抓取天氣情況

<?php
/**
 * Server 天氣情況獲取函數
 * @param unknown $city
 */
function getweather($city){
	
	$url = "http://www.weather.com.cn/weather1d/".$city.".shtml";
	$result = file_get_contents($url);
	
	$result = explode("<li class='dn on' data-dn='todayT'>",$result);
	$result = explode("<div class=\"m m2\" id=\"2_3d\">",$result[1]);
	
	$result = mb_convert_encoding($result[0],"GBK","UTF-8");
	
	//$result = explode("</i> </p>",$result);
	
	$first = explode("<div class=\"slid\"></div>",$result);
	//print_r($first);exit;
	
	$w1 = get_we_info($first[0]);
	$w2 = get_we_info($first[1]);
	
	$week = mb_substr($w1[0],-2);
	
	echo "煙臺天氣預報 [今天] 星期$week $w1[1] 至   $w2[1] $w1[2]轉 $w2[2] $w1[3]/$w2[3]";
	
}
/**
 * 每天的詳細天氣狀況(今天 白天->今天 夜間)
 * @param unknown $info
 * @return string
 */
function get_we_info($info){
	$w_info=explode("</h1>",$info);
	$we_arr[0] = strip_tags($w_info[0]);
	
	$w_info=explode("</h2>",$w_info[1]);
	$we_arr[1] = strip_tags($w_info[0]);
	
	$w_info = explode("<p class=\"tem\">",$w_info[1]);
	$we_arr[2] = strip_tags($w_info[0]);
	
	$w_info=explode("<p class=\"win\">",$w_info[1]);
	$we_arr[3]=strip_tags($w_info[0]);
	return $we_arr;
}

getweather(101120501);

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章