當前位置:首頁 » 操作系統 » 天氣源碼

天氣源碼

發布時間: 2024-03-28 21:14:11

『壹』 php怎麼抓取天氣預報

可以藉由php的api或者preg_match_all偷偷擷取去達成目的

這里給你一段我給台灣朋友有一段源碼

<?php

header("Content-Type:text/html;charset=utf-8");
functiongetWeather($city){

$toURL="
$city.htm";
$post=array();
$ch=curl_init();
$options=array(
CURLOPT_REFERER=>'',
CURLOPT_URL=>$toURL,
CURLOPT_VERBOSE=>0,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_USERAGENT=>"Mozilla/4.0(compatible;)",
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query($post),
);
curl_setopt_array($ch,$options);

$result=curl_exec($ch);
curl_close($ch);
//連接中央氣象局
echo'<pre>';
preg_match_all('/<tableclass="FcstBoxTable01"[^>]*[^>]*>(.*)</div>/si',$result,$matches,PREG_SET_ORDER);

preg_match_all('/<tdnowrap="nowrap"[^>]*[^>]*>(.*)</td>/si',$matches[0][1],$m1,PREG_SET_ORDER);

$m2=explode('</td>',$m1[0][1]);
//print_r($m2);//取得每日資料m2[0~6]

$weather=array();
for($i=0;$i<=6;$i++){

preg_match_all('/src=[^>]*[^>](.*)/si',$m2[$i],$m5,PREG_SET_ORDER);//取得天氣圖檔
$m6=explode('"',$m5[0][0]);
$wi='
($m6[1],'../../');
$wtitle=$m6[3];
print_r($wtitle);
$weather[$i]['date']=date("m-d",mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
$weather[$i]['temperature']=trim(strip_tags($m2[$i]));
$weather[$i]['title']=$wtitle;
$weather[$i]['img']=$wi;
}

return($weather);

}

$weather=getWeather("Taipei_City");
print_r($weather);


//header("Location:loc.php");
?>

首先

$toURL="http://www.cwb.gov.tw/V7/forecast/taiwan/inc/city/$city.htm";

這里是讀取資料的網址

上面的是台灣中央氣象局

	preg_match_all('/<tableclass="FcstBoxTable01"[^>]*[^>]*>(.*)</div>/si',$result,$matches,PREG_SET_ORDER);

preg_match_all('/<tdnowrap="nowrap"[^>]*[^>]*>(.*)</td>/si',$matches[0][1],$m1,PREG_SET_ORDER);

這里是截取台灣中央氣象局網頁信息table class="FcstBoxTable01" [^>]*[^>]*>(.*)</div>的資料以及<td nowrap="nowrap" [^>]*[^>]*>(.*)</td>的資料分別是1天跟1周

$m2=explode('</td>',$m1[0][1]);
//print_r($m2);//取得每日資料m2[0~6]

這里是取得每日的資料

preg_match_all('/src=[^>]*[^>](.*)/si',$m2[$i],$m5,PREG_SET_ORDER);//取得天氣圖檔

這里是取得天氣的圖檔

		$m6=explode('"',$m5[0][0]);
$wi='
($m6[1],'../../');
$wtitle=$m6[3];
print_r($wtitle);
$weather[$i]['date']=date("m-d",mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
$weather[$i]['temperature']=trim(strip_tags($m2[$i]));
$weather[$i]['title']=$wtitle;
$weather[$i]['img']=$wi;

這里是返回的網址,日期,標題,圖檔等等的資料

$weather=getWeather("Taipei_City");
print_r($weather);

然後這里是顯示出地區的一周天氣預報


結論:就是如果你想從網站上面截取天氣預報

在php可以是用preg_match_all(網頁的表格table,表格的列數tr,表格的欄位td,或者更加廣泛的標簽div等等獲取)

『貳』 ASP天氣預報系統源碼,速求

<%
Response.ContentType="text/html; charset=gb2312"

Call weather()

Sub weather()

url="http://weather.news.qq.com/inc/07_dc107.htm" '莆田的天氣

Call IsObjInstalled("Microsoft.XMLHTTP")

weatherStr= getHTTPPage(url)

if weatherStr="" then
response.write "抱歉,天氣預報載入失敗!"
else
set reg=new Regexp
reg.Multiline=True
reg.Global=false
reg.IgnoreCase=true
reg.Pattern="<td height=""57"" align=""center"" bgcolor=""#EEF3F8"">((.|\n)*?)</td></tr>"

Set matches = reg.execute(weatherStr)
For Each match1 in matches
weatherStr=match1.Value
Next
Set matches = Nothing
Set reg = Nothing

if InStr(weatherStr,"沒有找到與")>0 then
response.write "抱歉,天氣預報載入失敗!"
Else
weatherStr=Replace(weatherStr,"<br>"," ")
%>
<font color="#CC0000" style="font-size: 9pt">今日天氣:<%=weatherStr%></font>
<%
end if

end if

End Sub

'// 採用 Microsoft.XMLHTTP 組件採集數據
Function getHTTPPage(url)
'on error resume next
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytes2BSTR(Http.responseBody)
set http=nothing
if err.number<>0 then err.Clear
End function

'// 採用 ADODB.Stream 處理採集到的數據,把二進制的文件轉成文本字元
Function Bytes2bStr(vin)
Dim BytesStream,StringReturn
Set BytesStream = Server.CreateObject("ADODB.Stream")
BytesStream.Type = 2
BytesStream.Open
BytesStream.WriteText vin
BytesStream.Position = 0
BytesStream.Charset = "GB2312"
BytesStream.Position = 2
StringReturn =BytesStream.ReadText
BytesStream.close
Set BytesStream = Nothing
Bytes2bStr = StringReturn
End Function

'//檢查組件,採用xmlhttp抓取網頁還是AspHTTP

Function IsObjInstalled(strClassString)
' On Error Resume Next
IsObjInstalled = False
Err = 0
Dim xTestObj
Set xTestObj = Server.CreateObject(strClassString)

If 0 = Err Then
If AspHttpOpen=1 Then
IsObjInstalled = True
Response.write "系統不支持 XMLHTTP 組件"
'Response.write "當前組件 ASPHTTP"
response.end()
Else
IsObjInstalled = False
'Response.write "當前組件 XMLHTTP"
End If
Else
IsObjInstalled = False
'Response.write "當前組件 XMLHTTP"
End If

Set xTestObj = Nothing
Err = 0

End Function
%>

熱點內容
airpod安卓怎麼切換下一曲 發布:2024-04-28 14:23:03 瀏覽:834
百姓網源碼 發布:2024-04-28 14:18:56 瀏覽:892
war包防止反編譯 發布:2024-04-28 14:17:16 瀏覽:327
linuxll命令 發布:2024-04-28 14:16:27 瀏覽:859
阿里雲伺服器增強安全配置取消 發布:2024-04-28 14:16:12 瀏覽:866
war3存儲空間不足 發布:2024-04-28 13:20:54 瀏覽:949
微信密碼已經忘記了如何找回 發布:2024-04-28 11:54:13 瀏覽:304
騰訊雲伺服器可以備案幾個網站 發布:2024-04-28 11:54:12 瀏覽:458
影響編譯速度的因素有哪些 發布:2024-04-28 11:53:58 瀏覽:255
安全配置汽車有哪些 發布:2024-04-28 11:48:07 瀏覽:830