vb 用 Internet Controls 请求到一网页后,如何取得其 html 内容?
就象用 IE 的 “查看源文件”功能一样看到的文本内容,也就是那些文字与html标记。我需要取得网页的源文件后分析里面的文字,获取需要的资料。
那些文字没有放在控件中(如果在控件中就好引用了),只是用 <td> 、<p> 、<font> 等标记的一些文字,并且不确定格式,只能逐字分析。

解决方案 »

  1.   

    用 Inet控件下载的一段代码Private Sub Download(UrtFile As String, LocalFile As String)
    Dim bData() As Byte      '数据变量
    Dim intFile As Integer   '可用文件变量
    Dim i As Integer
    Dim ttt As String
    Inet1.Tag = "Work"
        intFile = FreeFile()      '将 intFile 设置为未使用的文件
        ' OpenURL 方法的结果首先传入 Byte 数组,
        '然后将 Byte 数组保存到磁盘。
        On Error Resume Next
        Kill LocalFile
        On Error GoTo 0
        bData() = Inet1.OpenURL(UrtFile, icByteArray)
        Open LocalFile For Binary Access Write As #intFile
            Put #intFile, , bData()
        Close #intFile
        Inet1.Tag = ""
        Exit Sub
        
    Err1:
    MsgBox "error!"
    Resume
    End Sub
      

  2.   

    偶的最牛的代码:)'获取网页源码
    Private Function strGetWebPageSource(strWebPageHTTP As String, thisInet As Inet) As String
        Dim tmpSourceStr As String
        'thisInet设置
        If theblGetURL Then                 '正在获取网页源码,不再获取
            Exit Function
        End If
        theblGetURL = True                  '正在获取网页源码标志
        Select Case theiUseDownType
            Case 2          '第二种获取源码的方法
                theWebPageSource2 = ""
                thisInet.Execute strWebPageHTTP, "GET"
                Do While True
                    If theStop Then
                        theblGetWebPageError = True
                        Exit Do
                    End If
                    If Not (Len(theWebPageSource2) = 0) Then     '获取到源码
                        If theblGetWebPageError Then
                            tmpSourceStr = ""
                        Else
                            tmpSourceStr = theWebPageSource2
                        End If
                        Exit Do
                    End If
                    DoEvents
                    Call ZZG_Sleep(1)
                Loop
            Case Else
                '取网页
                tmpSourceStr = thisInet.OpenURL(strWebPageHTTP, 0)
                If theblGetWebPageError Then
                    tmpSourceStr = ""
                End If
                
        End Select
        strGetWebPageSource = tmpSourceStr
        theblGetURL = False                 '获取网页源码结束标志
    End Function'Inet设置
    Private Function blInetSet(thisInet As Inet) As Boolean
        If theProxyIP = "" Then             '没有代理
            thisInet.AccessType = icUseDefault
            thisInet.Proxy = ""
        Else
            thisInet.AccessType = icNamedProxy
            thisInet.Proxy = theProxyIP & ":" & theProxyPort
        End If
        blInetSet = True
    End Function'获取网页时的状态
    Private Sub MyInet_StateChanged(ByVal State As Integer)
        Dim tmpMessage As String
        tmpMessage = "▁▅█☉当前状态:"
        Select Case State
            Case 1
                theMessageText = tmpMessage & "正在查询所指定的主机的 IP 地址"
            Case 2
                theMessageText = tmpMessage & "成功地找到所指定的主机的 IP 地址"
            Case 3
                theMessageText = tmpMessage & "正在与主机连接"
            Case 4
                theMessageText = tmpMessage & "已与主机连接成功"
            Case 5
                theMessageText = tmpMessage & "正在向主机发送请求"
            Case 6
                theMessageText = tmpMessage & "发送请求已成功"
            Case 7
                theMessageText = tmpMessage & "在接收主机的响应"
            Case 8
                theMessageText = tmpMessage & "成功地接收到主机的响应"
            Case 9
                theMessageText = tmpMessage & "正在解除与主机的连接"
            Case 10
                theMessageText = tmpMessage & "已成功地与主机解除了连接"
            Case 11
                theMessageText = tmpMessage & "与主机通讯时出现了错误"
                theWebPageSource2 = "Err"
                theblGetWebPageError = True
            Case 12
                Select Case theiUseDownType
                    Case 2      'FTP
                        Dim tmpByte() As Byte
                        Dim tmpTempFilePath As String
                        Dim tmpID As Integer
                        tmpTempFilePath = thePath & "tmpS.tmp"
                        tmpID = FreeFile
                        Open tmpTempFilePath For Binary Access Write As #tmpID
                            tmpByte = MyInet.GetChunk(1024, icByteArray)
                            Do While UBound(tmpByte) > 0
                                Put #tmpID, , tmpByte
                                tmpByte = MyInet.GetChunk(1024, icByteArray)
                                DoEvents
                            Loop
                            Put #tmpID, , tmpByte
                        Close #tmpID
                        theWebPageSource2 = GetTextFileToStr(tmpTempFilePath)
                        Call ZZG_blDeleteFile(tmpTempFilePath)
                End Select
                theMessageText = tmpMessage & "请求已经完成,并且所有数据均已接收到"
        End Select
        Call blWriteInfo(theMessageText)
        Me.Refresh
    End Sub
      

  3.   

    谢谢楼上!
    请问 Inet 控件是什么来着?我用 microsoft internet controls ,即 webbrowser 控件,它没有 openURL 方法。
      

  4.   

    Private Sub Command1_Click()
    Dim b() As Byte
    '将读取的 HTML 数据放进一个 byte array.
    b() = Inet1.OpenURL(, icByteArray)
    '建立一个暂存文件來存放取回來的 html 文件
    Open Text2.text For Binary Access Write As #1
    Put #1, , b()
    Close #1
    End Sub
      

  5.   

    是Microsoft Internet Transfer Control 6.0控制你那个控件做浏览器还不错,但单独下载就不怎样了!
      

  6.   

    老大们用的都是 inet 控件。我不是用来下载,我是要访问国家气象局的网面,指令它查询某城市的天气。所以用 webbrowser 打开,想将打开的结果页分析一下得到有用的数据。
      

  7.   

    如果 webbrowser 不好使,看来我得改用 inet 控件了。
      

  8.   

    http://jinesc.6600.org/myweb/disp.asp?idd=1182&room=10 ☆一个提取火车时刻表的程序☆ 
     这个是我做的,  你可以参考下里面的代码  用的就是 inet 控件
      

  9.   

    还有  http://jinesc.6600.org/myweb/disp.asp?idd=1169&room=10   ☆VB如何发送信息到一个web地址☆ 
    有这2个你应该能做出来了
      

  10.   

    感谢楼上各位,研究中...如果大家有直接用webbrowser 得到源文件的方法,请告诉我。
    否则,我可能得考虑换用 inet 控件了。
      

  11.   

    这一篇可以满足你
    自动上网抓数据的机器人 
    http://dev.csdn.net/develop/article/17/17697.shtm
    参考
    http://dev.csdn.net/article/28/28377.shtm
    http://dev.csdn.net/article/28/28376.shtm
    http://dev.csdn.net/article/28/28374.shtm
    http://dev.csdn.net/article/28/28375.shtmhttp://www.applevb.com/qa/qa000257.htm也不一定要分析文件,可以通过webbrowser的对象来做
      

  12.   

    楼上老大提供的文章看来是我需要的,正在阅读。感谢ing...
      

  13.   

    //如果大家有直接用webbrowser 得到源文件的方法,请告诉我。
    否则,我可能得考虑换用 inet 控件了。以前讨论过很多次了,自己在论坛内搜索以下