精美而实用的网站,关注web编程技术、网站运营、SEO推广,让您轻松愉快的学习

小偷程序的原理,就是读取目标网站的指定网页内容,分析出想要的部分,然后按自己的要求过滤不需要的内容,最后入库或者重新生成新的HTML

青岛星网给出最简单的范例代码供初学者学习

<%
'ASP常用的采集函数
'1、读取目标网页内容,输入url网址,返回getHTTPPage后获得的html代码
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
'2、编码转换,因为直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
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
%>

使用上面的函数,读取被采集网页的内容,这里是读取青岛星网首页的全部源代码,最后输出,若有乱码,你也可以使用BytesToBstr函数转换内容:

<%
Dim Url,Html
Url="http://www.qdxw.net"
Html = getHTTPPage(Url)
Response.write Html
%>

青岛星网温馨提醒:此代码是最简单的入门学习参考。

Tags:ASP XMLHTTP 小偷