当前位置:首页 » 操作系统 » 天气源码

天气源码

发布时间: 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
%>

热点内容
传说对决上传照片 发布:2024-11-28 05:49:18 浏览:464
解压笔怎么画 发布:2024-11-28 05:49:17 浏览:517
预编译查询 发布:2024-11-28 05:32:35 浏览:312
山东青岛iptv设置密码是多少 发布:2024-11-28 05:30:26 浏览:315
小鹏的解压 发布:2024-11-28 05:10:07 浏览:21
ibm存储售后 发布:2024-11-28 05:00:39 浏览:32
python监控进程脚本 发布:2024-11-28 04:54:47 浏览:180
android加载html页面 发布:2024-11-28 04:54:38 浏览:901
噻苯隆如何配置 发布:2024-11-28 04:53:56 浏览:395
普通电脑改服务器风道风罩 发布:2024-11-28 04:52:28 浏览:454