为何XMLHTTP 60 req没有显示整个HTML文档?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (120)

我试图从一个网站获得HTML文档和其他。

但我无法获得与网页相关的整个HTML文档。我的调试器输出语句不会像我想的那样显示整个网页,它会被打断。

我的代码如下:

Const SecForm4 As String = "https://www.secform4.com/significant-buys.htm"

Sub LoadWebPage()

    Dim XMLReq As New MSXML2.XMLHTTP60

    XMLReq.Open "GET", SecForm4, False
    XMLReq.send

    If XMLReq.Status <> 200 Or XMLReq.readyState <> 4 Then
        MsgBox "Problem" & vbNewLine & XMLReq.Status & "-" & XMLReq.statusText
        Exit Sub
    End If

    ParsingHTMLDocument XMLReq.responseText

End Sub

Sub ParsingHTMLDocument(HTMLText As String)

    Dim HTMLDoc As New MSHTML.HTMLDocument

    HTMLDoc.body.innerHTML = HTMLText
    Debug.Print HTMLText

End Sub
提问于
用户回答回答于

下面的工作是抓取文档和表格。不太可能将整个文档打印到即时窗口,因为它对容量有限制(https://social.msdn.microsoft.com/Forums/office/en-US/5c2a83cf-0596-41a2-9f42-e64cc6b1e556/immediate-window-memory-limitation?forum=accessdev)。相反,你可以写入文本文件并进行检查。

换个文件,"C:\Users\User\Desktop\Test.txt"

Option Explicit
Public Sub GetInfo()
    Dim sResponse As String, i As Long, html As New HTMLDocument, hTable As HTMLTable
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.secform4.com/significant-buys.htm", False
        .Send
        sResponse = StrConv(.responseBody, vbUnicode)
    End With
    sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
    WriteTxtFile sResponse
    With html
        .body.innerHTML = sResponse
        Set hTable = .getElementById("filing_table")
        MsgBox hTable.localName
    End With
End Sub

 Public Sub WriteTxtFile(ByVal aString As String, Optional ByVal filePath As String = "C:\Users\User\Desktop\Test.txt")
    Dim fso As Object, Fileout As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Fileout = fso.CreateTextFile(filePath, True, True)
    Fileout.Write aString
    Fileout.Close
End Sub

需要引用HTML对象库。

扫码关注云+社区

领取腾讯云代金券