xmlhttp对象抓取事例

来源:互联网 发布:java 用户行为记录 编辑:程序博客网 时间:2024/04/30 09:27
XMLHTTP对象及其方法
------------------
MSXML中提供了Microsoft.XMLHTTP对象,能够完成从数据包到Request对象的转换以及发送任务。
创建XMLHTTP对象的语句如下:
Set objXML = CreateObject("Msxml2.XMLHTTP") 或
Set objXML = CreateObject(“Microsoft.XMLHTTP”)
' Or, for version 3.0 of XMLHTTP, use:
' Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
对象创建后调用Open方法对Request对象进行初始化,语法格式为:
poster.open http-method, url, async, userID, password
Open方法中包含了5个参数,前三个是必要的,后两个是可选的(在服务器需要进行身份验证时提供)。参数的含义如下所示: 
http-method: HTTP的通信方式,比如GET或是 POST
url: 接收XML数据的服务器的URL地址。通常在URL中要指明 ASP或CGI程序
async: 一个布尔标识,说明请求是否为异步的。如果是异步通信方式(true),客户机就不等待服务器的响应;如果是同步方式(false),客户机就要等到服务器返回消息后才去执行其他操作
userID 用户ID,用于服务器身份验证
password 用户密码,用于服务器身份验证
XMLHTTP对象的Send方法
用Open方法对Request对象进行初始化后,调用Send方法发送XML数据:
poster.send XML-data
Send方法的参数类型是Variant,可以是字符串、DOM树或任意数据流。发送数据的方式分为同步和异步两种。在异步方式下,数据包一旦发送完毕,就结束Send进程,客户机执行其他的操作;而在同步方式下,客户机要等到服务器返回确认消息后才结束Send进程。
XMLHTTP对象中的readyState属性能够反映出服务器在处理请求时的进展状况。客户机的程序可以根据这个状态信息设置相应的事件处理方法。属性值及其含义如下表所示:
值 说明
0 Response对象已经创建,但XML文档上载过程尚未结束
1 XML文档已经装载完毕
2 XML文档已经装载完毕,正在处理中
3 部分XML文档已经解析
4 文档已经解析完毕,客户端可以接受返回消息
客户机处理响应信息
客户机接收到返回消息后,进行简单的处理,基本上就完成了C/S之间的一个交互周期。客户机接收响应是通过XMLHTTP对象的属性实现的:
● responseTxt:将返回消息作为文本字符串;
● responseXML:将返回消息视为XML文档,在服务器响应消息中含有XML数据时使用;
● responseStream:将返回消息视为Stream对象。 
 

<%

hehe = Hello("http://mmsg.qq.com/cgi-bin/gddylist?Type=13&Sort=1&Page=3", "<html>", "</html>", ".*(<td width=""35%"" bgcolor=""#[/dABCDE]{6}"">(.*)</td>)[./n]*", "<font style=""font-size:9pt;"" color=blue>$2</font><br>")
response.Write hehe

Function Hello(strUrl, strStart, strEnd, patrn, replStr)
    Str = GetBody(strUrl)
    Str = MyMid(Str, strStart, strEnd)
    Str = ReplaceTest(patrn, replStr, Str)
    Hello = Str
End Function

Function MyMid(Str, strstart, strend)
    If strstart = "" Then
        i = 0
    Else
        i = InStr(Str, strstart)
    End If
    If strend = "" Then
        j = Len(Str)
    Else
        j = InStr(i, Str, strend)
    End If
    MyMid = Mid(Str, i, j - i + 1)
End Function

Function ReplaceTest(patrn, replStr, str1)
    Dim regEx, match, matches
    Set regEx = New RegExp
    regEx.Pattern = patrn
    regEx.IgnoreCase = True
    regEx.Global = True
    Set matches = regEx.Execute(str1)
    For Each match in matches
        ReplaceTest = ReplaceTest&regEx.Replace(Match.Value, replStr)
    Next
End Function

Function GetBody(Url)
    Set objXML = CreateObject("Microsoft.XMLHTTP")
    With objXML
        .Open "Get", Url, False, "", ""
        .SEnd
        GetBody = .ResponseBody
    End With
    GetBody = BytesToBstr(GetBody, "GB2312")
    Set objXML = Nothing
End Function

Function BytesToBstr(strBody, CodeBase)
    Set objStream = Server.CreateObject("Adodb.Stream")
    With objStream
        .Type = 1
        .Mode = 3
        .Open
        .Write strBody
        .Position = 0
        .Type = 2
        .Charset = CodeBase
        BytesToBstr = .ReadText
        .Close
    End With
    Set objStream = Nothing
End Function
%>
其他调用示例:
hehe = Hello("http://list.mp3.baidu.com/song/A.htm", "<table width=""90%"" border=""0"" align=""center"" cellpadding=""3"" cellspacing=""0"" bgcolor=""#f5f5f5"" >", "<DIV align=center>", ".*(<td width=""20%""><a href="".*/.htm"" target=_blank>)(.*)(</a></td>)[./n]*", "<font style=""font-size:9pt;"" color=blue>$2</font><br>")