XMLHTTP可用于模拟http的GET和POST请求,我们可以用XMLHTTP写个函数实时抓取网站数据,现在的网站小偷程序都是用这方法实现的。今天青岛星网跟大家分享:ASP+XMLHTTP获取网页数据的方法。
'url是目标网页地址 www.qdxw.net 函数范例
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=bytesToBSTR(Http.responseBody,"gb2312")
set http=nothing
If Err.number<>0 then
Response.Write "<p align='center'><font color='red'><b>服务器获取文件内容出错</b></font></p>"
Err.Clear
End If
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青岛星网总结:有了上面2个函数,我们只需要在写一个创建文件函数,即可把抓取的网页自动创建生成一个HTML页面。
小伙伴又说不会写创建文件函数,青岛星网一并跟大家分享了。
function WriteIn(testfile,msg)
set fs=server.CreateObject("scripting.filesystemobject")
set thisfile=fs.CreateTextFile(testfile,True)
thisfile.Write(""&msg& "")
thisfile.close
set fs = nothing
end function

