有時候,需要獲取網頁的html代碼,如新聞采集。采集之前,首頁要得到被采集的頁面的Html代碼,然後根據代碼,分析出你想要得到的內容,最後將得到的內容保存到自己的數據庫中
function.ASP
<%
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
%>
index.ASP
<!--#include file="function.ASP"-->
<%
url="http://www.google.cn"
response.write getHTTPPage(url)
%>