XMLHttp ASP遠程擷取網頁內容代碼

來源:互聯網
上載者:User

複製代碼 代碼如下: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=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"
'轉換原來預設的UTF-8編碼轉換成GB2312編碼,否則直接用XMLHTTP調用有中文字元的網頁得到的將是亂碼
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"
'轉換原來預設的UTF-8編碼轉換成GB2312編碼,否則直接用XMLHTTP調用有中文字元的網頁得到的將是亂碼
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
end Function

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.