首页 > 解决方案 > 与 InternetExplorerMedium 一起使用的 MSXML2.ServerXMLHTTP.6.0 的 VBA 问题

问题描述

我喜欢尽可能使用 MSXML2.ServerXMLHTTP.6.0,因为它更快。但是,当我需要与网站交互时,我无法弄清楚如何使用它。那可能是另一个问题。

但是,当我使用以下代码时,我得到了所需的结果......

Sub GoogleSfund()

Set objIExplorer = New InternetExplorerMedium

    objIExplorer.Silent = True
    objIExplorer.Visible = False 'for testing change to true

    objIExplorer.Navigate "https://www.google.com/search?q=DWCPF"

    Do While objIExplorer.Busy Or Not objIExplorer.ReadyState = 4: DoEvents: Loop
    
    a = objIExplorer.Document.body.getElementsByTagName("g-card-section")
    pos1 = InStr(a.innerText, "INDEXDJX: DWCPF")
    pos2 = InStr(a.innerText, "Disclaimer")
    b = Mid(a.innerText, pos1, pos2 - pos1)
    b = Replace(b, vbCrLf & vbCrLf, vbCrLf)
    MsgBox b
    TSP_Test.lblSfund.Caption = b
objIExplorer = ""
End Sub...

使用 (MSXML2.ServerXMLHTTP.6.0) 它不会抓取具有相同 URL 的页面

Sub GoogleSfundFAST()
Dim sSourceUrl As String
Dim HttpReq As Object

Set HttpReq = CreateObject("MSXML2.ServerXMLHTTP.6.0")
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLH3Doc As New MSHTML.HTMLDocument
Dim HTMLInstProcDoc As New MSHTML.HTMLDocument

sSourceUrl = "https://www.google.com/search?q=dwcpf"
'sSourceUrl = "https://www.google.com/search"
HttpReq.Open "GET", sSourceUrl, False
HttpReq.send

If HttpReq.Status = 200 Then
    HttpReq.getAllResponseHeaders
    HTMLDoc.body.innerHTML = HttpReq.responseText
End If

Dim Obj As MSHTML.HTMLGenericElement
Dim Heading As MSHTML.IHTMLElementCollection
Dim HD As HTMLElementCollection

Debug.Print HTMLDoc.body.innerHTML

End Sub

有任何想法吗?

标签: htmlvbaweb-scraping

解决方案


推荐阅读