回 帖 发 新 帖 刷新版面

主题:如何能用asp获取其他网站的天气信息?

我想在网站做一个天气预报页面,自己添加数据太麻烦了,我下载了一个天气预报的小偷程序,但是有问题,运行后出现空白页,什么内容都没有
代码:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="conn.asp" -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>在线天气预报</title>
<link href="http://www.weathercn.com/forecastn/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<%
'声明一个函数,用于读取远程文件
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then 
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear 
end function
'声明一个函数,对读取的远程文件进行汉字转码
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText 
objstream.Close
set objstream = nothing
End Function

Dim Url, Html, Str_start, Str_end
'要偷的网页的地址,你只需要从http://www.weathercn.com/找到你要偷取的页面就行
Url="http://www.weathercn.com/forecast/citydetail.jsp?sta_id=56778"
Html = getHTTPPage(Url) '开始读取远程地址

Function gettext(patrn,strng)
  Dim regEx,Match,Matches,t()                ' 建立变量
  Set regEx = New RegExp            ' 建立正则表达式。
  regEx.Pattern = patrn            ' 设置模式。
  regEx.IgnoreCase = True        ' 设置区分大小写。
  regEx.Global = True            ' 搜索全部匹配。
  Set Matches = regEx.Execute(strng)    ' 执行搜索。
  i=0
  reDim t(Matches.count)
For Each Match in Matches      ' 循环遍历Matches集合。
    i=i+1
    t(i) =trim(Match.Value)
Next
End Function
patrn="<td class=""font_sl"">([^<]+)"
gettext patrn,Html 
for j=1 to i
%>
t(<%=j%>)=<%=t(j)%>
<%next%>

</body>
</html>

运行上面的代码后出现空白页面,我查看页面的代码如下:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>在线天气预报</title>
<link href="http://www.weathercn.com/forecastn/style.css" rel="stylesheet" type="text/css">
</head>
<body>


</body>
</html>



不知道是哪里的问题,大家帮忙看看程序这样写行不行。

我要获取的数据都是在一个<table>里,
一个共同的特点就是每个单元格的组成都是一样的:
<td class="font_sl">我要的数据</td>



我要获取的网页就是下面:
http://www.weathercn.com/forecast/citydetail.jsp?sta_id=56778

回复列表 (共2个回复)

沙发

我自己又试过了,前面的部分没有问题,可以得到网页的代码,问题应该是出在后面的正则表达式部分,就是:
Function gettext(patrn,strng)
  Dim regEx,Match,Matches,t()                ' 建立变量
  Set regEx = New RegExp            ' 建立正则表达式。
  regEx.Pattern = patrn            ' 设置模式。
  regEx.IgnoreCase = True        ' 设置区分大小写。
  regEx.Global = True            ' 搜索全部匹配。
  Set Matches = regEx.Execute(strng)    ' 执行搜索。
  i=0
  reDim t(Matches.count)
For Each Match in Matches      ' 循环遍历Matches集合。
    i=i+1
    t(i) =trim(Match.Value)
Next
End Function
patrn="<td class=""font_sl"">([^<]+)"
gettext patrn,Html 
for j=1 to i
%>

大家再帮忙看看吧。看看正则表达式有没有问题,其他的部分有没有问题。谢谢了

很多东西我还不是太理解比如matches,教程说是数组,但是用法是怎么样的呢?matches里面有Match.FirstIndex、Match.Value,如果我要调用第3个match的value,应该用什么语句呢?

板凳

自己把问题给解决了,结贴

我来回复

您尚未登录,请登录后再回复。点此登录或注册