asp下利用xmlhttp取得網頁內容的方法這個方法一般比較通用的,然後透過字元截取網頁的內容。
複製程式碼如下: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()'發送
If objXML.readystate<>4 then '判斷文件是否已解析完,以便做客戶端接受回傳訊息
exit function
End If
getHTTPPage=bBytesToBstr(objXML.responseBody)'傳回訊息,同時用函數定義編碼
set objXML=nothing'關閉
if err.number<>0 then err.Clear
End Function
Function Newstring(wstr,strng)
Newstring=Instrlc(wstring=Instrlcx ),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 =
Type
02
objstream.Charset = gb2312
'轉換原來預設的UTF-8編碼轉換成GB2312編碼,否則直接用XMLHTTP呼叫有中文字元的網頁得到的會是亂碼
bBytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
end Function
Function BytesToBstr(body)
dimobjstream
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
'轉換原來預設的UTF-8編碼轉換成GB2312編碼,否則直接用XMLHTTP呼叫有中文字元的網頁得到的將是亂碼
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
end Function