VBA:运行时错误424:尝试Web刮擦时所需的对象

时间:2017-09-13 06:58:22

标签: vba object

我正在尝试使用morgninstar.co.uk更新各种基金规模。代码工作正常,直到它突然停止并给出错误: “运行时错误424:需要对象”。 发生错误的确切行是:

Set allData = IE.document.getElementById("overviewQuickstatsDiv").getElementsByTagName("tbody")(0)

这个想法是最终扫描整个“tbody”-tag并在“tr”和“td”-tags中查找“Fund Size”行。当找到“基金大小”时,代码将返回第3个“td”标签(实际基金大小)。 在此之后,我将添加一个循环来循环我已经获得的资金列表。 由于代码完全停止工作,我还没有这么远。在这里,我只想检查代码是否返回实际的基金规模。 由于“tr”-tags中并不总是有3个“td”-tags,我仍然需要构建某种IF语句来解决这个问题。

但是现在我只想知道如何让代码再次运行?我花了很多时间寻找答案,但由于这似乎是一个变量型问题,解决方案取决于具体情况。

我正在使用Excel 2010和Internet Explorer 11。

以简单形式复制粘贴的网址: http://www.morningstar.co.uk/uk/funds/snapshot/snapshot.aspx?id=F0GBR04BKW

Sub testToScrapeWholeTbodyTag()

'Microsoft Internet Controls
'Microsoft HTML Object Library
'Microsoft Shell Controls and Automation


'======Opens URL======
Dim IE As Object
Set IE = CreateObject("internetexplorer.application")

With IE
    .navigate "http://www.morningstar.co.uk/uk/funds/snapshot/snapshot.aspx?id=F0GBR04BKW"
    .Visible = False
End With

While IE.Busy
    DoEvents
Wend


'======Got from internet, fixed a previous error. However, I'm not 100% sure what this does======
Dim sh
Dim eachIE As Object

Do
    Set sh = New Shell32.Shell
    For Each eachIE In sh.Windows
        If InStr(1, eachIE.LocationURL, "http://www.morningstar.co.uk/uk/funds/snapshot/snapshot.aspx?id=F0GBR04BKW") Then
            Set IE = eachIE
            IE.Visible = False  '"This is here because in some environments, the new process defaults to Visible."
            Exit Do
            End If
        Next eachIE
    Loop
Set eachIE = Nothing
Set sh = Nothing





'======Looks for the "Fund Size"======
'Trying to look for "Fund Size" inside "tr"-tag and if found, return the value in the 3rd "tr"-tag
Set allData = IE.document.getElementById("overviewQuickstatsDiv").getElementsByTagName("tbody")(0) 'Run-time error 424: Object required
row1 = allData.getElementsByTagName("tr")(5).Cells(0).innerHTML
row2 = allData.getElementsByTagName("tr")(5).Cells(1).innerHTML
row3 = allData.getElementsByTagName("tr")(5).Cells(2).innerHTML

    If Left(row1, 9) = "Fund Size" Then
        Worksheets("Sheet3").Range("B3") = Split(row3, ";")(1)
    End If

Debug.Print allData.getElementsByTagName("tr")(5).Cells(0).innerHTML '"Fund Size"
Debug.Print allData.getElementsByTagName("tr")(5).Cells(2).innerHTML 'Actual fund size


IE.Quit
Set IE = Nothing


End Sub

修改 切换方法。现在的问题是提取基金规模。所以下面的代码工作原样,但我需要添加几行来获得基金规模。这是我第一次使用这种方法,所以很可能我只是不理解一些非常基本的东西。尽管如此,我还是无法自己找到解决方案。

Sub XMLhttpRequestTest()

'Microsoft XML, v 6.0
'Microsoft HTML object library

Dim HTMLDoc As New HTMLDocument
Dim ohttp As New MSXML2.XMLHTTP60 

Dim myurl As String
Dim TRelements As Object
Dim TRelement As Object

myurl = "http://www.morningstar.co.uk/uk/funds/snapshot/snapshot.aspx?id=F0GBR04BKW"
ohttp.Open "GET", myurl, False
ohttp.send


HTMLDoc.body.innerHTML = ohttp.responseText

With HTMLDoc.body
    Set TRelements = .getElementsByTagName("tr")

    For Each TRelement In TRelements
        Debug.Print TRelement.innerText
    Next

End With


End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用

中的css selector
#overviewQuickstatsDiv td.line.text

然后选择索引4处的元素

#表示ID。 . = className。

Option Explicit
Public Sub XMLhttpRequestTest()
    'Microsoft XML, v 6.0
    'Microsoft HTML object library

    Dim HTMLDoc As New HTMLDocument, ohttp As New MSXML2.XMLHTTP60

    Const URL As String = "http://www.morningstar.co.uk/uk/funds/snapshot/snapshot.aspx?id=F0GBR04BKW"
    Dim TRelements As Object, TRelement As Object

    With ohttp
        .Open "GET", URL, False
        .send

        HTMLDoc.body.innerHTML = .responseText
        Debug.Print HTMLDoc.querySelectorAll("#overviewQuickstatsDiv td.line.text")(4).innerText
        'Your other stuff
    End With
End Sub