XMLHttp ASP遠程獲取網(wǎng)頁內(nèi)容代碼
更新時間:2008年11月10日 00:23:30 作者:
asp下利用xmlhttp獲取網(wǎng)頁內(nèi)容的方法這個方法一般比較通用的,然后通過字符截取網(wǎng)頁的內(nèi)容。
復(fù)制代碼 代碼如下:
url="http://www.csdn.net/"
wstr=getHTTPPage(url)
start=Newstring(wstr,"資源精選<!-- 下載 -->")
over=Newstring(wstr,"<div class=""friendlink"">")
body=mid(wstr,200,500)
response.write body
Function getHTTPPage(url)
dim objXML
set objXML=createobject("MSXML2.XMLHTTP")'定義
objXML.open "GET",url,false'打開
objXML.send()'發(fā)送
If objXML.readystate<>4 then '判斷文檔是否已經(jīng)解析完,以做客戶端接受返回消息
exit function
End If
getHTTPPage=bBytesToBstr(objXML.responseBody)'返回信息,同時用函數(shù)定義編碼
set objXML=nothing'關(guān)閉
if err.number<>0 then err.Clear
End Function
Function Newstring(wstr,strng)
Newstring=Instr(lcase(wstr),lcase(strng))
if Newstring<=0 then Newstring=Len(wstr)
End Function
Function bBytesToBstr(body)
dim objstream
set objstream = CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = "gb2312"
'轉(zhuǎn)換原來默認的UTF-8編碼轉(zhuǎn)換成GB2312編碼,否則直接用XMLHTTP調(diào)用有中文字符的網(wǎng)頁得到的將是亂碼
bBytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
end Function
Function BytesToBstr(body)
dim objstream
set objstream = CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = "utf-8"
'轉(zhuǎn)換原來默認的UTF-8編碼轉(zhuǎn)換成GB2312編碼,否則直接用XMLHTTP調(diào)用有中文字符的網(wǎng)頁得到的將是亂碼
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
end Function
相關(guān)文章
光碟工具 Alcohol 120% v1.9.6.4719 下載(附序列號注冊碼)
光碟工具 Alcohol 120% v1.9.6.4719 下載(附序列號注冊碼)...2007-02-02獨孤劍寫的馬克斯迅雷片庫采集插件1.4 官方最新版提供下載了
非常不錯的采集迅雷插件,經(jīng)本人測試非常不錯,而且作者很熱心,是個好人,希望大家支持下,不要拿去賣了。2008-01-01Asp WinHttp.WinHttpRequest.5.1 對象使用詳解 偽造 HTTP 頭信息
使用 WinHttpRequest 偽造 HTTP 頭信息,偽造 Referer 等信息2012-01-01