gpt4 book ai didi

html - 单击 "submit"后 VBA Excel 拉取新的网页数据

转载 作者:太空宇宙 更新时间:2023-11-04 16:27:08 25 4
gpt4 key购买 nike

我正在尝试从一个按 API 编号提供油井数据的网站中提取一些信息(API 是美国每口油井的唯一编号)

网站:http://sonlite.dnr.state.la.us/sundown/cart_prod/cart_con_wellapi1

API 示例:1708300502

问题是,当我到达第二页时,IE.document.getElementsByTagName("body")(0).innerText 仍然从初始页面返回数据。如何获取更新后的页面数据?

最终目标是到达第2页,通过IE.document.getElementsByTagName("a")(0)点击“30570”。点击然后阅读最后的第3页。我只是不知道如何阅读更新后的页面:(

Option Explicit

Sub sonris_WellData()
Dim IE As InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

Dim i As Integer

'Open SONRIS website
Application.StatusBar = "Opening Website"
IE.navigate "http://sonlite.dnr.state.la.us/sundown/cart_prod/cart_con_wellapi1"
Do While IE.readyState <> 4: DoEvents: Loop
Application.Wait Now() + TimeValue("00:00:01")
Application.StatusBar = False

IE.document.forms(0).p_apinum.Value = "1708300502" 'plug-in API
IE.document.forms(0).submit

' Wait until the next page opens
Application.StatusBar = "Opening Website"
Do While IE.readyState <> 4: DoEvents: Loop
Application.Wait Now() + TimeValue("00:00:01")
Application.StatusBar = False

' Read the page - this is where the issue occurs, MsgBox keeps returning text from the very 1st page
MsgBox IE.document.getElementsByTagName("body")(0).innerText

IE.Quit
End Sub

最佳答案

这似乎有效。而不是 DoEvents 使用 WinAPI Sleep 函数。我还在表单提交后添加了对 Sleep 函数的调用。

我们经常看到由某些 javascript 等动态提供服务的网站,在这些情况下,浏览器可能看起来是READYSTATE_COMPLETE 或不是Busy,但页面尚未呈现"new"结果。

Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub sonris_WellData()
Dim IE As Object 'InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

Dim i As Integer

'Open SONRIS website
Application.StatusBar = "Opening Website"
IE.navigate "http://sonlite.dnr.state.la.us/sundown/cart_prod/cart_con_wellapi1"
Do While IE.readyState <> 4
Sleep 1000
Loop

Application.StatusBar = False

IE.document.forms(0).p_apinum.Value = "1708300502" 'plug-in API
IE.document.forms(0).submit

Sleep 1000

' Wait until the next page opens
Application.StatusBar = "Opening Website"
Do While IE.readyState <> 4
Sleep 1000
Loop

Application.StatusBar = False

' Read the page - this is where the issue occurs, MsgBox keeps returning text from the very 1st page
MsgBox IE.document.getElementsByTagName("body")(0).innerText

IE.Quit
End Sub

您可以在 .submit 之后尝试使用稍长的 Sleep

或者,我注意到在您提交后,URL 发生了变化,因此您也可以尝试将第二个等待循环更改为:

Do While IE.LocationURL ="http://sonlite.dnr.state.la.us/sundown/cart_prod/cart_con_wellapi1"
Sleep 1000
Loop

这应该让 Excel.Application 等待 URL 更改。

或者,使用 XMLHTTPRequest 可能会更好(在 SO 和互联网上的其他地方有很多这样的例子)。这允许您像浏览器一样发送请求,而无需实际使用网络浏览器。然后您可以简单地将返回文本解析为 HTML 或 XML。为此,我会使用 Microsoft XML v6.0 库引用。

关于html - 单击 "submit"后 VBA Excel 拉取新的网页数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701118/

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