获取不同网站数据示例程序
TSD 2005-04-01 10:32:03 获取不同网站数据示例程序:
在网站制作中有时需要获取不同网站的数据(变量),本程序通过XMLHTTP来实现这一功能,方便实用
tsd_w1.htm(放在网站一)
---------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>这是一个测试存放变量的页面</title>
</head>
<body>
<!--
程序:SD
WEB:http://www.fanzny.com
Email:fanzny@21cn.com
QQ:61192909
-->
<p> </p>
<p> </p>
<p align="center">这是一个测试存放变量的页面</p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center">例如:[sd]存在网站一中的变量[/sd]</p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center">说明:变量存在[sd]和[/sd]标记内</p>
</body>
</html>
tsd_w2.asp(放在网站二)
---------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>这是一个测试获取变量的页面</title>
</head>
<body>
<!--
程序:SD
WEB:http://www.fanzny.com
Email:fanzny@21cn.com
QQ:61192909
-->
<p> </p>
<p> </p>
<p align="center">这是一个获取变量的页面</p>
<p align="center"> </p>
<%
'1.获取网页内容
Function getHTTPPage(url)
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=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
End function
'2.编码转换
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 GetCode,GetUrl,s,e,l
GetUrl="http://www.0086sex.com/tsd_w1.htm"
GetCode=getHTTPPage(GetUrl)
GetCode=trim(GetCode)
if instr(GetCode,"无法找到网页")>0 then
response.write "<center>变量未获取到,可能是网络的问题,请重试!"
response.end
else
s=instr(GetCode,"[sd]")+4
e=instr(GetCode,"[/sd]")
l=e-s
response.write "<center>获取到的变量:<br><br>" & mid(GetCode,s,l)
end if
%>
</body>
</html>
此程序已在网络上测试通过,如果你有更好的办法欢迎指教!谢谢!!
联系方式:Email:fanzny@21cn.com QQ:61192909