国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > ASP > 正文

Asp 使用 Microsoft.XMLHTTP 抓取網頁內容并過濾需要的

2020-07-27 12:44:34
字體:
來源:轉載
供稿:網友
Asp 使用 Microsoft.XMLHTTP 抓取網頁內容(沒用亂碼),并過濾需要的內容

示例源碼:
復制代碼 代碼如下:

<%
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 抓取主要內容
strBody = GetBody(strHTML,"<div id=""Div_newsContentc"" class=""cnt"">","</div>",0,0)
strBody =Replace(strBody,"(本文首發于","")
strBody =Replace(strBody,"財富動力網</a>,轉載請注明出處。)","")
strBody =Replace(strBody,"本文首發于,轉載請注明出處。)","")
strBody =Replace(strBody,"財富動力網</a>:http://www.927953.com","")
strBody =Replace(strBody,"本文首發于","")

Response.Write RegRemoveHref(strBody)

REM 獲取對應網址響應的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"

'轉換原來默認的UTF-8編碼轉換成GB2312編碼,否則直接用
'XMLHTTP調用有中文字符的網頁得到的將是亂碼
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function


REM 使用正則表達式,抓取之內標記的內容
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
%>

效果圖如下: 
 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桃园县| 安岳县| 青河县| 四会市| 淮阳县| 延寿县| 蚌埠市| 武威市| 荣昌县| 凌源市| 大理市| 葫芦岛市| 寿阳县| 阳朔县| 正阳县| 林口县| 青海省| 巴里| 德令哈市| 固始县| 临清市| 尼玛县| 武强县| 潮安县| 旬邑县| 山丹县| 中西区| 潼关县| 时尚| 天镇县| 宜君县| 平邑县| 赤城县| 岗巴县| 饶阳县| 清水县| 屏南县| 鄄城县| 柘荣县| 开阳县| 霍邱县|