首页后端开发ASP.NETAsp 使用 Microsoft.XMLHTTP 抓取网页内容并过滤需要的

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

时间2024-02-07 15:29:03发布访客分类ASP.NET浏览644
导读:收集整理的这篇文章主要介绍了Asp 使用 Microsoft.XMLHTTP 抓取网页内容并过滤需要的,觉得挺不错的,现在分享给大家,也给大家做个参考。 Asp 使用 Microsoft...
收集整理的这篇文章主要介绍了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 获取对应网址响应的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
%>

效果图如下: 
  您可能感兴趣的文章:
  • 用asp+xmlhttp编写web采集程序
  • asp.net HttpWebRequest自动识别网页编码
  • Asp.net XMLHTTP封装类(GET,Post发送和接收数据)
  • 运行asp.net时出现 http错误404-文件或目录未找到
  • Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
  • asp.net利用HttpModule实现防sql注入
  • asp.net 模拟提交有文件上传的表单(通过http模拟上传文件)
  • asp页面提示Response 对象 错误 ASP 0156 : 80004005 HTTP 头错误
  • Asp WinHttp.WinHttpRequest.5.1 对象使用详解 伪造 HTTP 头信息
  • asp中利用xmlhttp抓取网页内容的代码
  • asp中使用MSXML2.ServerXMLHTTP实现异步请求例子
  • 解决ASP中http状态跳转返回错误页的问题

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Asp 使用 Microsoft.XMLHTTP 抓取网页内容并过滤需要的
本文地址: https://pptw.com/jishu/604221.html
asp textbox获取显示mysql数据示例代码 asp中for循环的使用方法

游客 回复需填写必要信息