运行时错误“424”:与webscrape所需的对象

我试图从VBA的网站上刮掉37.59%: –

<span id="dgCompShare_Label23_5">37.59%</span> 

我设法导航到网站,并进行各种select。 以下是我的相关VBA代码。

 Dim appIE As Object Dim f As Object Dim ws As Worksheet Dim wb As Workbook Set wb = Application.Workbooks("Macro2") Set ws = wb.Worksheets("Sheet1") Set appIE = CreateObject("internetexplorer.application") With appIE .Navigate "https://website.com/" .Visible = True Do While appIE.Busy DoEvents Loop Set f = appIE.document.getElementbyid("dgCompShare_Label23_5") If Not f Is Nothing Then percent = f.innerText With ws .Cells(4, 3) = percent End With 

我收到运行时错误“424”:所需的对象:设置f = appIE.document.getElementbyid(“dgCompShare_Label23_5”)我不明白为什么会发生这种情况。

它发生在页面没有完全加载或内部加载,并尝试设置一个对象。

如果您确定要查找的元素在html页面上可用,请尝试一下,看看这是否适合您。

 On Error Resume Next Do While appIE.Busy And f Is Nothing DoEvents Set f = appIE.document.getElementbyid("dgCompShare_Label23_5") Loop Percent = f.innerText 

或者也检查ieApp对象的ReadyState,然后设置对象variables…

 Do While appIE.busy Or appid.readystate <> 4 DoEvents Loop Set f = appIE.document.getElementbyid("dgCompShare_Label23_5")