<%@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>

<%
'判断数据库里是否有当天的天气预报
set rs1=server.createobject("adodb.recordset")
sql="select * from wether where day=date()" 
rs1.open sql,conn,1,3
if not rs1.eof then
response.Write rs1("wether")
rs1.close
set rs1=nothing
else
'如果没有，就读取未能远程数据并保存在数据库内
%>
<%
'声明一个函数，用于读取远程文件
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/forecastn/forcast/forecastDetail.jsp?station_name=慈溪&station_id=58467&province=浙江&district=宁波"

Html = getHTTPPage(Url) '开始读取远程地址

'搜索要偷取的内容的开始位置
Str_start = instr(Html, "国内城市预报")
Str_start = instr(Str_start, Html, "<table width=""770"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"">")

'搜索要偷取的内容的结束位置
Str_end = instr(Str_start, Html, "<table width=""100"" border=""0"" cellspacing=""0"" cellpadding=""0"">")

'截取内容
Html = Mid(Html, Str_start, Str_end - Str_start)

'修改宽度
Html = Replace(Html, "<table width=""770"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"">", "<table width=""760"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"">")
'过滤内容中的图片地址
Html = Replace(Html, "images/", "http://www.weathercn.com/forecastn/forcast/images/")
'修改宽度
Html = Replace(Html, "<table width=""770"" border=""0"" cellpadding=""0"" cellspacing=""0"">", "<table width=""760"" border=""0"" cellpadding=""0"" cellspacing=""0"">")
'修改宽度
Html = Replace(Html, "<table width=""739""  border=""0"" cellspacing=""0"" cellpadding=""0"">", "<table width=""729""  border=""0"" cellspacing=""0"" cellpadding=""0"">")
'输出偷取的内容
response.Write Html
'写入数据库
set rs=server.createobject("adodb.recordset")
sql="select * from wether where (id is null)" 
rs.open sql,conn,1,3
rs.addnew
rs("wether")=Html
rs("day")=date()
rs.update
rs.close
set rs=nothing
end if 
%>
</body>
</html>