Asp 使用 Microsoft.XMLHTTP 抓取網(wǎng)頁內(nèi)容并過濾需要的
更新時(shí)間:2014年05月14日 09:08:51 作者:
這篇文章主要介紹了Asp 使用 Microsoft.XMLHTTP 抓取網(wǎng)頁內(nèi)容(沒用亂碼),并過濾需要的內(nèi)容
Asp 使用 Microsoft.XMLHTTP 抓取網(wǎng)頁內(nèi)容(沒用亂碼),并過濾需要的內(nèi)容
示例源碼:
<%
Dim xmlUrl,http,strHTML,strBody
xmlUrl = Request.QueryString("u")
REM 異步讀取XML源
Set http = server.CreateObject("Microsoft.XMLHTTP")
http.Open "POST",xmlUrl,false
http.setrequestheader "User-Agent", "Mozilla/4.0"
http.setrequestheader "Connection", "Keep-Alive"
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send()
strHTML = BytesToBstr(http.ResponseBody)
set http = nothing
REM 抓取主要內(nèi)容
strBody = GetBody(strHTML,"<div id=""Div_newsContentc"" class=""cnt"">","</div>",0,0)
strBody =Replace(strBody,"(本文首發(fā)于","")
strBody =Replace(strBody,"財(cái)富動(dòng)力網(wǎng)</a>,轉(zhuǎn)載請(qǐng)注明出處。)","")
strBody =Replace(strBody,"本文首發(fā)于,轉(zhuǎn)載請(qǐng)注明出處。)","")
strBody =Replace(strBody,"財(cái)富動(dòng)力網(wǎng)</a>:http://www.927953.com","")
strBody =Replace(strBody,"本文首發(fā)于","")
Response.Write RegRemoveHref(strBody)
REM 獲取對(duì)應(yīng)網(wǎng)址響應(yīng)的HTML
Function BytesToBstr(body)
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 = "UTF-8"
'轉(zhuǎn)換原來默認(rèn)的UTF-8編碼轉(zhuǎn)換成GB2312編碼,否則直接用
'XMLHTTP調(diào)用有中文字符的網(wǎng)頁得到的將是亂碼
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
REM 使用正則表達(dá)式,抓取之內(nèi)標(biāo)記的內(nèi)容
Function GetBody(ConStr,StartStr,OverStr,IncluL,IncluR)
If ConStr="$False$" or ConStr="" or IsNull(ConStr)=True Or StartStr="" or IsNull(StartStr)=True Or OverStr="" or IsNull(OverStr)=True Then
GetBody="$False$"
Exit Function
End If
Dim ConStrTemp
Dim Start,Over
ConStrTemp=Lcase(ConStr)
StartStr=Lcase(StartStr)
OverStr=Lcase(OverStr)
Start = InStrB(1, ConStrTemp, StartStr, vbBinaryCompare)
If Start<=0 then
GetBody="$False$"
Exit Function
Else
If IncluL=False Then
Start=Start+LenB(StartStr)
End If
End If
Over=InStrB(Start,ConStrTemp,OverStr,vbBinaryCompare)
If Over<=0 Or Over<=Start then
GetBody="$False$"
Exit Function
Else
If IncluR=True Then
Over=Over+LenB(OverStr)
End If
End If
GetBody=MidB(ConStr,Start,Over-Start)
End Function
REM 過濾a超鏈接
Function RegRemoveHref(HTMLstr)
Set ra = New RegExp
ra.IgnoreCase = True
ra.Global = True
ra.Pattern = "<a[^>]+>(.+?)<\/a>"
RegRemoveHref = Replace(ra.replace(HTMLstr,"$1"),"href=""http://www.927953.com""","")
END Function
%>
效果圖如下:
示例源碼:
復(fù)制代碼 代碼如下:
<%
Dim xmlUrl,http,strHTML,strBody
xmlUrl = Request.QueryString("u")
REM 異步讀取XML源
Set http = server.CreateObject("Microsoft.XMLHTTP")
http.Open "POST",xmlUrl,false
http.setrequestheader "User-Agent", "Mozilla/4.0"
http.setrequestheader "Connection", "Keep-Alive"
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send()
strHTML = BytesToBstr(http.ResponseBody)
set http = nothing
REM 抓取主要內(nèi)容
strBody = GetBody(strHTML,"<div id=""Div_newsContentc"" class=""cnt"">","</div>",0,0)
strBody =Replace(strBody,"(本文首發(fā)于","")
strBody =Replace(strBody,"財(cái)富動(dòng)力網(wǎng)</a>,轉(zhuǎn)載請(qǐng)注明出處。)","")
strBody =Replace(strBody,"本文首發(fā)于,轉(zhuǎn)載請(qǐng)注明出處。)","")
strBody =Replace(strBody,"財(cái)富動(dòng)力網(wǎng)</a>:http://www.927953.com","")
strBody =Replace(strBody,"本文首發(fā)于","")
Response.Write RegRemoveHref(strBody)
REM 獲取對(duì)應(yīng)網(wǎng)址響應(yīng)的HTML
Function BytesToBstr(body)
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 = "UTF-8"
'轉(zhuǎn)換原來默認(rèn)的UTF-8編碼轉(zhuǎn)換成GB2312編碼,否則直接用
'XMLHTTP調(diào)用有中文字符的網(wǎng)頁得到的將是亂碼
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
REM 使用正則表達(dá)式,抓取之內(nèi)標(biāo)記的內(nèi)容
Function GetBody(ConStr,StartStr,OverStr,IncluL,IncluR)
If ConStr="$False$" or ConStr="" or IsNull(ConStr)=True Or StartStr="" or IsNull(StartStr)=True Or OverStr="" or IsNull(OverStr)=True Then
GetBody="$False$"
Exit Function
End If
Dim ConStrTemp
Dim Start,Over
ConStrTemp=Lcase(ConStr)
StartStr=Lcase(StartStr)
OverStr=Lcase(OverStr)
Start = InStrB(1, ConStrTemp, StartStr, vbBinaryCompare)
If Start<=0 then
GetBody="$False$"
Exit Function
Else
If IncluL=False Then
Start=Start+LenB(StartStr)
End If
End If
Over=InStrB(Start,ConStrTemp,OverStr,vbBinaryCompare)
If Over<=0 Or Over<=Start then
GetBody="$False$"
Exit Function
Else
If IncluR=True Then
Over=Over+LenB(OverStr)
End If
End If
GetBody=MidB(ConStr,Start,Over-Start)
End Function
REM 過濾a超鏈接
Function RegRemoveHref(HTMLstr)
Set ra = New RegExp
ra.IgnoreCase = True
ra.Global = True
ra.Pattern = "<a[^>]+>(.+?)<\/a>"
RegRemoveHref = Replace(ra.replace(HTMLstr,"$1"),"href=""http://www.927953.com""","")
END Function
%>
效果圖如下:
您可能感興趣的文章:
- 用asp+xmlhttp編寫web采集程序
- asp.net HttpWebRequest自動(dòng)識(shí)別網(wǎng)頁編碼
- Asp.net XMLHTTP封裝類(GET,Post發(fā)送和接收數(shù)據(jù))
- 運(yùn)行asp.net時(shí)出現(xiàn) http錯(cuò)誤404-文件或目錄未找到
- Javascript+XMLHttpRequest+asp.net無刷新讀取數(shù)據(jù)庫數(shù)據(jù)
- asp.net利用HttpModule實(shí)現(xiàn)防sql注入
- asp.net 模擬提交有文件上傳的表單(通過http模擬上傳文件)
- asp頁面提示Response 對(duì)象 錯(cuò)誤 ASP 0156 : 80004005 HTTP 頭錯(cuò)誤
- Asp WinHttp.WinHttpRequest.5.1 對(duì)象使用詳解 偽造 HTTP 頭信息
- asp中利用xmlhttp抓取網(wǎng)頁內(nèi)容的代碼
- asp中使用MSXML2.ServerXMLHTTP實(shí)現(xiàn)異步請(qǐng)求例子
- 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁的問題
相關(guān)文章
ASP + Serv-u 實(shí)現(xiàn)FTP的代碼
ASP + Serv-u 實(shí)現(xiàn)FTP的代碼...2006-10-10
垃圾引用防治補(bǔ)丁以及發(fā)送引用修正補(bǔ)丁的自動(dòng)安裝程序
垃圾引用防治補(bǔ)丁以及發(fā)送引用修正補(bǔ)丁的自動(dòng)安裝程序...2007-02-02
一個(gè)非常簡(jiǎn)單的將半角轉(zhuǎn)換為中文的函數(shù)
一個(gè)非常簡(jiǎn)單的將半角轉(zhuǎn)換為中文的函數(shù)...2006-09-09
ReSaveRemoteFile函數(shù)之a(chǎn)sp實(shí)現(xiàn)查找文件保存替換的代碼
ReSaveRemoteFile函數(shù)之a(chǎn)sp實(shí)現(xiàn)查找文件保存替換的代碼...2007-09-09
Dom遍歷XML的一個(gè)例子,結(jié)果為樹狀結(jié)構(gòu)
Dom遍歷XML的一個(gè)例子,結(jié)果為樹狀結(jié)構(gòu)...2007-01-01
Server.Execute方法執(zhí)行指定的ASP程序
Server.Execute方法執(zhí)行指定的ASP程序...2007-09-09
簡(jiǎn)單的ASP統(tǒng)計(jì)制作實(shí)例
一個(gè)能對(duì)訪問者進(jìn)行編號(hào)、記錄訪問次數(shù)、IP、時(shí)間的統(tǒng)計(jì)制作實(shí)例,需要的朋友可以參考下2015-09-09

