MSXML2.XMLHTTP响应不同于IE文档

时间:2017-01-26 06:02:08

标签: vba excel-vba excel

我想知道为什么在某些情况下,来自MSXML2.XMLHTTP对象的响应中的HTML不会产生与自动化Internet Explorer和检查document属性值相同的结果。

例如,下面的过程将使用对象MSXML2.XMLHTTP(列A)找到的结果与使用InternetExplorer对象(列B)找到的结果进行比较。

InternetExplorer对象的结果包括预期的纳斯达克指数,但MSXML2.XMLHTTP对象的结果不包括纳斯达克指数,并且完全不同:

Sub ExtractDataFromInternet()
'Enable Tools/references 1. Microsoft Internet Control and 2. Microsoft HTML Object Library.

Dim URL
Dim objHTML As HTMLDocument
Dim Oelement As Object
Dim ie As New InternetExplorer
Dim J, Field1, Field2

    Set objHTML = New HTMLDocument
    URL = "http://www.nasdaq.com/"

    '-------- METHOD1: MSXML2.XMLHTTP --------------------------
    With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", URL, False
    .send
    objHTML.body.innerHTML = .responseText
    End With
    J = 0
    Set Field1 = objHTML.getElementsByTagName("td")
    For Each Oelement In Field1
    Worksheets("sheet1").Cells(J + 1, 1) = Field1(J).innerText
    J = J + 1
    Next Oelement

    '----------METHOD2: InternetExplorer Object----------------
    Set ie = New InternetExplorer
    With ie
    .navigate URL
    .Visible = False
    While .Busy Or .readyState <> READYSTATE_COMPLETE
    DoEvents
    Wend
    Set objHTML = .document
    DoEvents
    End With
    J = 0
    Set Field1 = objHTML.getElementsByTagName("td")
    For Each Oelement In Field1
    Worksheets("sheet1").Cells(J + 1, 2) = Field1(J).innerText
    J = J + 1
    Next Oelement
    DoEvents
    ie.Quit
    DoEvents
    Set ie = Nothing
    '----------------------------------------------
    End Sub

0 个答案:

没有答案