gpt4 book ai didi

vba - ExcelVBA - 通过 MSXML2.XMLHTTP 的 HttpReq - 加载页面后获取页面

转载 作者:行者123 更新时间:2023-12-04 20:45:52 25 4
gpt4 key购买 nike

我从基于内部 Web 的 Dataservice (cognos) 获取数据时遇到问题。
基本上,我整理了一个 GET 请求,例如“blah.com/cognosapi.dll?product=xxx&date=yyy...”,将其发送到服务器并接收一个网页,我可以将其存储为 HTML 并稍后解析为我的 excel 表单。

我构建了一个过去运行良好的 VBA 程序,但是 web 服务发生了变化,现在他们正在显示“您的报告正在运行”页面,持续时间从 1 秒到 30 秒。所以当我调用我的函数时,我总是下载这个“你的报告正在运行”页面而不是数据。如何捕捉“报告正在运行”页面后自动加载的页面?

这是以 GETstring 和目标路径为参数的 DownloadFile 函数。

Public Function DownloadFile(sSourceUrl As String, _
sLocalFile As String) As Boolean


Dim HttpReq As Object
Set HttpReq = CreateObject("MSXML2.XMLHTTP")

Dim HtmlDoc As New MSHTML.HTMLDocument


HttpReq.Open "GET", sSourceUrl, False
HttpReq.send


If HttpReq.Status = 200 Then
HttpReq.getAllResponseHeaders
HtmlDoc.body.innerHTML = HttpReq.responseText
Debug.Print HtmlDoc.body.innerHTML

End If

'Download the file. BINDF_GETNEWESTVERSION forces
'the API to download from the specified source.
'Passing 0& as dwReserved causes the locally-cached
'copy to be downloaded, if available. If the API
'returns ERROR_SUCCESS (0), DownloadFile returns True.

DownloadFile = URLDownloadToFile(0&, _
sSourceUrl, _
sLocalFile, _
BINDF_GETNEWESTVERSION, _
0&) = ERROR_SUCCESS

End Function

谢谢
大卫

最佳答案

最后你给了我最终的链接来解决我的问题。我将代码烘焙到我的 DownloadFile 函数中,以与 IE 对象保持一致直到结束,然后将其关闭。

我发现的一个错误是在对 HTMLObject 进行任何操作之前应该轮询 readystate。

Public Function DownloadFile(sSourceUrl As String, _
sLocalFile As String) As Boolean

Dim IE As InternetExplorer
Set IE = New InternetExplorer



Dim HtmlDoc As New MSHTML.HTMLDocument
Dim collTables As MSHTML.IHTMLElementCollection
Dim collSpans As MSHTML.IHTMLElementCollection
Dim objSpanElem As MSHTML.IHTMLSpanElement

Dim fnum As Integer

With IE
'May changed to "false if you don't want to see browser window"
.Visible = True
.Navigate (sSourceUrl)
'this waits for the page to be loaded
Do Until .readyState = 4: DoEvents: Loop
End With

'Set HtmlDoc = wait_for_html(sSourceUrl, "text/css")
Do
Set HtmlDoc = IE.Document

'searching for the "Span" tag
Set collSpans = HtmlDoc.getElementsByTagName("span")

'first Span element cotains...
Set objSpanElem = collSpans(0)

'... this if loading screen is display
Loop Until Not objSpanElem.innerHTML = "Your report is running."

'just grab the tables and leave the rest
Set collTables = HtmlDoc.getElementsByTagName("table")

fnum = FreeFile()
Open sLocalFile For Output As fnum ' save the file and add html and body tags
Print #fnum, "<html>"
Print #fnum, "<body>"

Print #fnum, collTables(15).outerHTML 'title
Print #fnum, collTables(17).outerHTML 'Date
Print #fnum, collTables(18).outerHTML 'Part, Operation etc.
Print #fnum, collTables(19).outerHTML 'Measuerements

Print #fnum, "</body>"
Print #fnum, "</html>"

Close #fnum
IE.Quit 'close Explorer

DownloadFile = True

End Function

关于vba - ExcelVBA - 通过 MSXML2.XMLHTTP 的 HttpReq - 加载页面后获取页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17230529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com