asp远程网页数据采集常用函数代码

时间:2008-10-31 12:44:00 

下面列出了asp远程网页数据采集程序中经常用到的函数,很实用,特别是正则表达式过滤函数。包括了使用xmlhttp采集远程网页内容,使用adodb.stream转换编码,内容过滤匹配等等..

数据采集程序

On Error Resume Next
Server.Scripttimeout=300
'采集远程数据
Function getHTTPData(url) 
    dim http 
    set http=Server.createobject("Msxml2.XMLHTTP")
    if instr(url,"http://")=0 then url="http://"&url
    Http.open "GET",url,false 
    Http.send() 
    if Http.Status<>200  then exit function 
    getHTTPData=bytesToBSTR(Http.responseBody,"UTF-8")
    set http=nothing
    if err.number<>0 then err.Clear
    sCharset="" 
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
'---------------------------------------------------------------------    
'服务器登录
Function login(url) 
    dim http 
    set http=Server.createobject("Msxml2.XMLHTTP")
    if instr(url,"http://")=0 then url="http://"&url
    Http.open "GET",url,false 
    Http.send() 
    if Http.Status<>200 then exit function 
    set http=nothing
    if err.number<>0 then err.Clear
End function
'---------------------------------------------------------------------
'正则替换
Function ReplaceText(fString,patrn, replStr)
    Set regEx = New RegExp
    regEx.Pattern = patrn
    regEx.IgnoreCase = True
    regEx.Global = True
    ReplaceText = regEx.Replace(fString, replStr)
End Function
'---------------------------------------------------------------------
'去标签 包括内容
Function ReplaceTag(str, tag)
    Set regEx = New RegExp
    regEx.Pattern = "<"&tag&"[^>]*?>.*?<\/"&tag&">"
    regEx.IgnoreCase = True
    regEx.Global = True
    ReplaceTag=regEx.Replace(str, "")
End Function
'---------------------------------------------------------------------    
'去标签 不包括内容
Function ReplaceTab(str, tag)
    Set regEx = New RegExp
    regEx.Pattern = "<\/?"&tag&"[^>]*>"
    regEx.IgnoreCase = True
    regEx.Global = True
    ReplaceTab=regEx.Replace(str, "")
End Function
'---------------------------------------------------------------------    
'去标签属性 保留标签
Function ReplaceinnerTag(str, tag)
    Set regEx = New RegExp
    regEx.Pattern = "(<\/?"&tag&")[^>]*>"
    regEx.IgnoreCase = True
    regEx.Global = True
    ReplaceinnerTag=regEx.Replace(str, "$1>")
End Function
'---------------------------------------------------------------------    
'按正则取数据
Function getText(fString, patrn,n) 
    dim Matches, tStr
    tStr = fString
    Set re = New Regexp
    re.IgnoreCase = True
    re.Global = True
    re.Pattern =  patrn
    set Matches = re.Execute(tStr)
    set re = nothing 
    rStr = ""
    For Each Match in Matches
        rStr = Match.SubMatches(n)
        exit for
    Next
    getText = rStr
End Function
'---------------------------------------------------------------------
'数据过滤
Function Encode_text(str)
    If Isnull(str) Then
        Encode_text = ""
        Exit Function 
    End If
    str = ReplaceText(str, "<\/?br[^>]*>" , vbCrlf )
    str = ReplaceText(str, "<\/?p[^>]*>" , vbCrlf )
    str = ReplaceTab(str, "[a-zA-Z]")
    str = ReplaceText(str, "\n\s*\r" ,Chr(10)&Chr(13))
    str = Replace(str, "&" , "&amp;" )
    str = Replace(str, ";" , ";" )
    str = Replace(str, "&amp;" , "&amp;" )
    str = Replace(str,Chr(34), "&quot;" )
    str = Replace(str, "'" , "'" )
    str = Replace(str, "<" , "&lt;" )
    str = Replace(str, ">" , "&gt;" )
    str = Replace(str, "(" , "(" )
    str = Replace(str, ")" , ")" )
    str = Replace(str, "*" , "*" )
    str = Replace(str, "%" , "%" )
    str = Replace(str,vbCrlf, "<br/>" )
    Encode_text = str
End Function
'---------------------------------------------------------------------
'通过Matches取数据
dim Matches
sub setMatches(str,sRe)
    Set re = New Regexp
    re.IgnoreCase = True
    re.Global = True
    re.Pattern =  sRe
    set Matches = re.Execute(str)
    set re=nothing 
end sub

'例子
call setMatches(textcontent, re)
For Each Match in Matches
    response.write Match.value
Next

 

标签:采集,函数,正则表达式,asp
0
投稿

猜你喜欢

  • 六条比较有用的MySQL数据库操作的SQL语句小结

    2024-01-26 21:00:46
  • python中如何实现径向基核函数

    2023-11-28 02:48:45
  • 使用线框图来简化你的产品设计流程

    2011-06-10 13:10:00
  • python抓取需要扫微信登陆页面

    2022-03-01 16:15:32
  • 儿童学习python的一些小技巧

    2021-10-14 05:09:40
  • python可变对象,不可变对象详解

    2023-10-18 05:14:25
  • 自己搭建resnet18网络并加载torchvision自带权重的操作

    2021-11-28 12:24:33
  • Docker配置PHP开发环境教程

    2023-08-17 04:19:49
  • IOS中safari下的select下拉菜单文字过长不换行的解决方法

    2024-04-25 13:07:58
  • Django更新models数据库结构步骤

    2024-01-16 09:05:25
  • 给JavaScript自定义一个Trim函数

    2008-04-20 16:30:00
  • 智能录入表格[适合BS模式项目的录入页面]

    2008-03-09 19:02:00
  • pandas行和列的获取的实现

    2022-06-05 07:57:49
  • 如何使用python批量修改文本文件编码格式

    2021-02-15 14:01:30
  • 几个javascript特效代码

    2010-04-23 20:39:00
  • 返回页面顶部top按钮通过锚点实现(自写)

    2024-04-10 10:47:23
  • Python 列表的基本操作介绍

    2021-09-09 08:13:34
  • 如何利用Vue3管理系统实现动态路由和动态侧边菜单栏

    2024-05-05 09:25:34
  • vue中实现拖动调整左右两侧div的宽度的示例代码

    2024-04-27 16:02:36
  • Python从文件中读取数据的方法步骤

    2023-05-05 07:45:32
  • asp之家 网络编程 m.aspxhome.com