分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

Asp 使用 Microsoft.XMLHTTP 抓取网页内容无乱码处理,并过滤须要的内容

发布时间:2023-09-06 01:36责任编辑:苏小强关键词:乱码
???????????????????????

Asp 使用 Microsoft.XMLHTTP 抓取网页内容。并过滤须要的内容

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 获取相应网址响应的HTMLFunction 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 = nothingEnd FunctionREM 使用正則表達式。抓取标签之内的的内容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 FunctionREM 过滤a超链接Function RegRemoveHref(HTMLstr) ?????Dim ClsTempLoseStr,regEx ????ClsTempLoseStr = Cstr(HTMLstr) ????Set regEx = New RegExp ????regEx.Pattern = "<(\/){0,1}a[^<>]*>" ????regEx.IgnoreCase = True ????regEx.Global = True ????ClsTempLoseStr = regEx.Replace(ClsTempLoseStr,"") ????RegRemoveHref = ClsTempLoseStr ????Set regEx = NothingEnd Function%>


效果图例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWltaXl1YW5nZ3Vhbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" ?/>

???????????????????
???

Asp 使用 Microsoft.XMLHTTP 抓取网页内容无乱码处理,并过滤须要的内容

原文地址:https://www.cnblogs.com/zhchoutai/p/8283861.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved