回 帖 发 新 帖 刷新版面

主题:帮忙看看我的代码,是哪里出问题了?

这个网页就是显示天气用的,具体思路是,首先根据现在的时间判断应该显示哪个数据,然后从数据库里面提取数据,如果没有数据,就从天气预报网站读取新的数据,写入数据库,然后再显示读取到的新数据,不同的部分我已经用不同的颜色标出,现在问题出来了,运行的时候提示:

错误类型:
Microsoft VBScript 编译器错误 (0x800A0411)
名称重定义
/weather.asp, line 84, column 4
dim d(80)
---^

我仔细看过了,确实有量的d(80),一处是在
<!--#include file="getweather.asp" -->页面里面,
另外一处是在读取数据库的时候,
但是这两个数组已经用

if     then
<!--#include file="getweather.asp" -->
else
d(80)
end if
这样的判断分开了,不可能同时出现,为什么还判断为错误呢
给我的感觉就是if的两种情况同时出现了,这怎么可能发生呢?

<!--#include file="getweather.asp" -->
这个页面没有问题,我测试过了,代码放在楼下。





代码如下:
<%
[color=008000]dim now_time_year,now_time_mouth,now_time_day,now_time_hour,now_time_minute,record_time_date
Set regEx = New RegExp            ' 建立正则表达式。
    regEx.Pattern = "(\d{4})-(\d{1,2})-(\d{1,2}).(\d{1,2}):(\d{1,2})"        ' 设置模式。
    regEx.IgnoreCase = True        ' 设置区分大小写。
    regEx.Global = True            ' 搜索全部匹配。
    Set Matches = regEx.Execute(now)    ' 执行搜索。
    For Each Match in Matches  ' 循环遍历Matches集合。
    now_time_year=Match.submatches(0)
    now_time_month=Match.submatches(1)
    now_time_day=CLng(Match.submatches(2))
    now_time_hour=CLng(Match.submatches(3))
    now_time_minute=CLng(Match.submatches(4))
    Next
set regEx=nothing


'20:30以后
if now_time_hour>20 or (now_time_hour=20 and now_time_minute>=30) then
now_time_hour=20
record_time_date=cdate(now_time_year&amp;"-"&amp;now_time_month&amp;"-"&amp;now_time_day&amp;" "&amp;now_time_hour&amp;":00:00")
end if 
'8:30-12:30
if 8<now_time_hour<12 or (now_time_hour=8 and now_time_minute>=30) or (now_time_hour=12 and now_time_minute<30) then
now_time_hour=08
record_time_date=cdate(now_time_year&amp;"-"&amp;now_time_month&amp;"-"&amp;now_time_day&amp;" "&amp;now_time_hour&amp;":00:00")
end if 
'12:30-20:30
if 12<now_time_hour<20 or (now_time_hour=12 and now_time_minute>=30) or (now_time_hour=20 and now_time_minute<30) then
now_time_hour=12
record_time_date=cdate(now_time_year&amp;"-"&amp;now_time_month&amp;"-"&amp;now_time_day&amp;" "&amp;now_time_hour&amp;":00:00")
end if 
'8:30以前
if now_time_hour<8 or (now_time_hour=8 and now_time_minute<30) then
now_time_hour=20
now_time_day=now_time_day-1
record_time_date=cdate(now_time_year&amp;"-"&amp;now_time_month&amp;"-"&amp;now_time_day&amp;" "&amp;now_time_hour&amp;":00:00")
end if [/color]

[color=000080]if Request.ServerVariables("Query_String")="" then 
cityid=56778
else
cityid=request.QueryString("cityid")
end if[/color]

dsn="dsn=kmsc; uid=sa; pwd=810723;"
sql="select * from 短期天气预报 where cityid='"&amp;cityid&amp;"' and 日期1='"&amp;record_time_date&amp;"'"
set rs = Server.CreateObject("ADODB.recordset")
rs.open sql,dsn,1,1
[color=800000]if rs.eof or rs.bof then%>
<!--#include file="getweather.asp" -->[/color]
<%
[color=FF00FF]else
dim record_time_year,record_time_month,record_time_day,record_time_hour,record_time_minute
    dim d(80)
    Set regEx = New RegExp            ' 建立正则表达式。
    regEx.Pattern = "\d{4}-(\d{1,2})-(\d{1,2}).(\d{1,2})"        ' 设置模式。
    regEx.IgnoreCase = True        ' 设置区分大小写。
    regEx.Global = True            ' 搜索全部匹配。
    Set Matches = regEx.Execute(rs("日期1"))    ' 执行搜索。
    For Each Match in Matches  ' 循环遍历Matches集合。
    d(0)=Match.submatches(0)
    d(1)=Match.submatches(1)
    d(2)=Match.submatches(2)
    Next
    Set regEx = nothing    
            
    Set regEx = New RegExp            ' 建立正则表达式。
    regEx.Pattern = "\d{4}-(\d{1,2})-(\d{1,2}).(\d{1,2})"        ' 设置模式。
    regEx.IgnoreCase = True        ' 设置区分大小写。
    regEx.Global = True            ' 搜索全部匹配。
    Set Matches = regEx.Execute(rs("日期2"))    ' 执行搜索。
    For Each Match in Matches  ' 循环遍历Matches集合。
    d(3)=Match.submatches(0)
    d(4)=Match.submatches(1)
    d(5)=Match.submatches(2)
    Next
    Set regEx = nothing    
    for i=6 to 14
    d(i)=rs(i-3)
    next
    d(16)=rs(12)
    dim temp(15)
    temp=split(trim(rs(13)),"|")    
    for i=20 to 35
    d(i)=temp(i-20)
    next
rs.close
set rs=nothing
end if
[/color]%>

<table width="800" border="0" cellpadding="0" cellspacing="0">
<tr><td colspan="2" height="36" background="images/top1bg.gif" class="smalltxt">&nbsp;</td>
</tr>
<tr>
<td width="550" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="smalltxt">
  <tr>
    <td colspan="2" height="35" background="images/wth1.jpg" style="font-size:12px">&nbsp; &nbsp; 今天是:<%=year(date)%>年<%=month(date)%>月<%=day(date)%>日 <%=WeekdayName(Weekday(Date))%>&nbsp; 现在的时间是:<%=hour(date)%>时<%=minute(date)%>分<%=second(date)%>秒
<a href="editweather.asp">编辑天气</a></td>
  </tr>
  <tr>
    <td colspan="2" class="cross" style="font-size:20px; font-weight:bold; color:#60afe7">[color=FF0000]<%=d(19)%>[/color]</td>
  </tr>
  <tr>
    <td class="leftb" width="33%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><%=d(19)%> (海拔:<%=d(18)%>)</td>
  </tr>
  <tr>
    <td><img alt="<%=d(6)%>" src="/image/weather/big/<%=d(6)%>.gif" width="60" height="60"><%if d(7)<>"" then%><img alt="<%=d(7)%>" src="/image/weather/big/<%=d(7)%>.gif" width="60" height="60"><%end if%></td>
  </tr>
  <tr>
    <td><%=d(8)%>℃~<%=d(9)%>℃</td>
  </tr>
</table></td>
    <td class="rightb" width="67%">&nbsp;&nbsp;
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td colspan="2"><%=year(date)%>年<%=d(0)%>月<%=d(1)%>日<%=d(2)%>时至<%=year(date)%>年<%=d(3)%>月<%=d(4)%>日<%=d(5)%>时</td>
          </tr>
        <tr>
          <td width="230">&nbsp;风力:<%=d(10)%></td>
          <td>&nbsp;日出:<%=d(11)%></td>
        </tr>
        <tr>
          <td>&nbsp;空气质量<%=d(12)%></td>
          <td>&nbsp;日落:<%=d(13)%></td>
        </tr>
        <tr>
          <td>&nbsp;污染物:<%=d(14)%></td>
          <td>&nbsp;东经:<%=d(15)%></td>
        </tr>
        <tr>
          <td>&nbsp;紫外线强度<%=d(16)%></td>
          <td>&nbsp;北纬:<%=d(17)%></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td colspan="2" class="cross">
    <table width="100%" border="0" cellpadding="5" cellspacing="0">
      <tr>
        <td width="85">项目</td>
        <td width="28">指数</td>
        <td>说明</td>
      </tr>
      <tr>
        <td>舒适度指数:</td>
        <td><%=d(20)%></td>
        <td><%=d(28)%></td>
      </tr>
      <tr>
        <td>穿衣指数:</td>
        <td><%=d(21)%></td>
        <td><%=d(29)%></td>
      </tr>
      <tr>
        <td>晨练指数:</td>
        <td><%=d(22)%></td>
        <td><%=d(30)%></td>
      </tr>
      <tr>
        <td>紫外线指数:</td>
        <td><%=d(23)%></td>
        <td><%=d(31)%></td>
      </tr>
      <tr>
        <td>洗车指数:</td>
        <td><%=d(24)%></td>
        <td><%=d(32)%></td>
      </tr>
      <tr>
        <td>感冒指数:</td>
        <td><%=d(25)%></td>
        <td><%=d(33)%></td>
      </tr>
      <tr>
        <td>中暑指数:</td>
        <td><%=d(26)%></td>
        <td><%=d(34)%></td>
      </tr>
      <tr>
        <td>晾晒指数:</td>    
        <td><%=d(27)%></td>
        <td><%=d(35)%></td>
      </tr>
    </table>    </td>
  </tr>
  <tr>
    <td colspan="2" height="3" bgcolor="#60afe7" style="line-height:7px">&nbsp;</td>
  </tr>
</table>
</td>
<td width="250" valign="top">&nbsp;</td>
</tr>
</table>

回复列表 (共3个回复)

沙发

这个就是getweather.asp的代码,运行过,没有问题。
<%
'声明一个函数,用于读取远程文件
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
Url="http://www.weathercn.com/forecast/citydetail.jsp?sta_id=56778"
Html = getHTTPPage(Url) '开始读取远程地址
    Dim regEx,Match,Matches 
'获取近期天气
'获取日期    
Set regEx = New RegExp
regEx.Pattern = "<td colspan=""3"" class=""font_red""><strong>(\d+)月(\d+)日(\d+)时至(\d+)月(\d+)日(\d+)时"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
dim d(80)
For Each Match in Matches 
for i=0 to 5
d(i)=Match.SubMatches(i)
Next
next
set regEx = nothing
'获取日期    
'获取气象    
Set regEx = New RegExp
regEx.Pattern = "<img alt=""([\u4e00-\u9fa5]*)"" src=""/forecasticon/60/[\w\.]+"" width=""60"" height=""60"">"regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)    
i=6
For Each Match in Matches 
d(i)=Match.SubMatches(0)
i=i+1
Next
set regEx = nothing
'获取气象    
'获取温度
Set regEx = New RegExp
regEx.Pattern = "([-]?\d+)℃~([-]?\d+)℃"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)    
i=8
For Each Match in Matches 
d(i)=Match.SubMatches(0)
i=i+1
d(i)=Match.SubMatches(1)
Next
set regEx = nothing
'获取温度    
'获取天气详情
Set regEx = New RegExp
regEx.Pattern = "<td[ width=""176""| width=""96""]* class=""font_sl"">([\u4e00-\u9fa5]*:([^<]+)|([\u4e00-\u9fa5]+))"
regEx.IgnoreCase = True
regEx.Global = True    
Set Matches = regEx.Execute(html)
i=10
For Each Match in Matches   
if match.submatches(1)="" then
d(i)=Match.SubMatches(2)
else
d(i)=match.submatches(1)
end if
i=i+1 
Next
set regEx = nothing     
'获取天气详情
'获取城市
Set regEx = New RegExp
regEx.Pattern = "{([\u4e00-\u9fa5]+)"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
For Each Match in Matches                 
d(19)=Match.SubMatches(0)
Next
set regEx = nothing     
'获取城市
'获取指数
Set regEx = New RegExp
regEx.Pattern = "(<td align=""center"">|<td align=""center""><span class=""font_sl"">|<td align=""center"" class=""qushi""><span class=""font_sl"">)(&nbsp;)?([^<]+)"    
regEx.IgnoreCase = True
regEx.Global = True    
Set Matches = regEx.Execute(html)
i=20
For Each Match in Matches 
d(i)=Match.SubMatches(2)
i=i+1 
Next
set regEx = nothing    
'获取指数
'获取指数内容
Set regEx = New RegExp
regEx.Pattern = "(<td><span class=""font_sl"">|<td class=""qushi""><span class=""font_sl"">)(&nbsp;)?([^<]+)"    
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=28
For Each Match in Matches 
d(i)=trim(Match.SubMatches(2))
i=i+1 
Next
set regEx = nothing      
'获取指数内容
'获取近期天气

板凳

'获取中期天气
'日期
Set regEx = New RegExp
regEx.Pattern = "l""><strong>(\d+)年(\d+)月(\d+)日"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=36
For Each Match in Matches
d(i)=trim(Match.SubMatches(0))
i=i+1
d(i)=trim(Match.SubMatches(1))
i=i+1
d(i)=trim(Match.SubMatches(2))
i=i+7
Next
set regEx = nothing      
'日期
'气象
Set regEx = New RegExp
regEx.Pattern = ">(<img alt=""([\u4e00-\u9fa5]+)"" src=""/forecasticon/20/[\w\.]+"" width=""20"" height=""20"">)(<img alt=""([\u4e00-\u9fa5]+)"" src=""/forecasticon/20/[\w\.]+"" width=""20"" height=""20"">)?"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=39
For Each Match in Matches
d(i)=trim(Match.SubMatches(1))
i=i+1 
d(i)=trim(Match.SubMatches(3))
i=i+8
Next
set regEx = nothing      
'气象
'温度
Set regEx = New RegExp
regEx.Pattern = "<strong>(\d+)<strong>℃</strong></strong></span><strong>~(\d+)"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=41
For Each Match in Matches
d(i)=trim(Match.SubMatches(0))
i=i+1 
d(i)=trim(Match.SubMatches(1))
i=i+8
Next
set regEx = nothing      
'温度
'风力
Set regEx = New RegExp
regEx.Pattern = "<td width=""121"" align=""center"" class=""font_sl"">([\u4e00-\u9fa5]*)"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=43
For Each Match in Matches 
d(i)=trim(Match.SubMatches(0))
i=i+9
Next
set regEx = nothing      
'风力
'趋势
Set regEx = New RegExp
regEx.Pattern = "<img src=""image/([\w]+).gif"" >"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=44
For Each Match in Matches 
d(i)=trim(Match.SubMatches(0))
i=i+9
Next
set regEx = nothing
'趋势
'获取中期天气    
'获取天气实况
'获取日期
Set regEx = New RegExp
regEx.Pattern = "d""><strong>(\d+)年(\d+)月(\d+)"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=72
For Each Match in Matches 
d(i)=Match.SubMatches(0)
i=i+1
d(i)=Match.SubMatches(1)
i=i+1
d(i)=Match.SubMatches(2)
Next
set regEx = nothing
'获取日期                
'获取时间                
Set regEx = New RegExp
regEx.Pattern = "i"">[^\d]+(\d+[\d\u4e00-\u9fa5]+)&nbsp;</td>"regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=75
For Each Match in Matches 
d(i)=trim(Match.SubMatches(0))
Next
set regEx = nothing
'获取时间
'获取温度、气像
Set regEx = New RegExp
regEx.Pattern = "[^\d]+([+|-]?\d+)℃[^<]+<img alt=""([\u4e00-\u9fa5]+)"    
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=76
For Each Match in Matches 
d(i)=trim(Match.SubMatches(0))
i=i+1
d(i)=trim(Match.SubMatches(1))
Next
set regEx = nothing
'获取温度、气像
'获取风力
Set regEx = New RegExp
regEx.Pattern = "<td  class=""blog_xhui"">[^\u4e00-\u9fa5]*[\u4e00-\u9fa5]+[^\d]?(\d*)"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=78
For Each Match in Matches
d(i)=trim(Match.SubMatches(0))
Next
set regEx = nothing
'获取风力
'获取能见度                
Set regEx = New RegExp
regEx.Pattern = """ class=""blog_xhui"">[^\d]+(\d+)公里"    
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(html)
i=79
For Each Match in Matches
d(i)=trim(Match.SubMatches(0))
Next
set regEx = nothing
'获取能见度                
'获取天气实况

for i=0 to 79
response.Write("第"&i&"个:"&d(i)&"<br>")
next

%>    

3 楼

楼主试一下,将变量的定义放在IF的前面,然后IF里面不进行定义。

我来回复

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