使用excel vba解析HTML时出错

时间:2022-11-19 21:57:24

So due to constraints, I need to parse some ugly html with excel vba. the problem with the HTML is that it has no element IDs. I have a page that has many unlabeled tables that each have a couple rows. The only thing I can build from is that there is an identifier in one of the cells that I need to pull. Every time the ID "xtu_id" appears as a value in a cell in a row of a table, I want to pull the data from that row. So it looks like this:

由于受到限制,我需要用excel vba解析一些难看的html。HTML的问题在于它没有元素id。我有一个页面有许多未标记的表,每个表有几行。我唯一能做的就是在其中一个单元格中有一个标识符。每次当ID“xtu_id”在一个表中的单元格中显示为一个值时,我想从该行中提取数据。看起来是这样的:

<tr>



<td>

                    col1

</td>


<td>

                    col2

</td>


<td>

                    xtu_id

</td>


<td>

                    col4

</td>


</tr>  

Now that I see xtu_id exists in this row, I want to dump all cells of that row into an excel sheet. Here is what I used from reading other * posts:

现在我看到xtu_id存在于这一行,我想将这一行的所有单元格转储到excel表中。以下是我在阅读其他*文章时用到的:

Sub CommandButton1_Click()

    Dim appIE As InternetExplorerMedium
    Set appIE = New InternetExplorerMedium

    With appIE
        .Navigate "https://my_website"
        .Visible = True
    End With

    Do While appIE.Busy Or appIE.ReadyState <> 4
        DoEvents
    Loop

    Set mydata = appIE.Document.getElementsByTagName("tr")

    For Each e In mydata
        For Each c In e
            If c.Cells().innerText Like "xtu_id" Then
                myValue = c.Cells().innerText
                MsgBox (myValue)
            End If
        Next c
    Next e
    Set appIE = Nothing

End Sub

This code works until I get to the [for each...] statement, I have trouble looping through each cell of each row to search for the "xtu_id" text. Any ideas on how to do this?

这个代码可以工作,直到我找到[for each…]语句,我在遍历每一行的每个单元格以搜索“xtu_id”文本时遇到了麻烦。有什么办法吗?

1 个解决方案

#1


1  

Try this:

试试这个:

For Each c In e.Cells
    If c.innerText Like "xtu_id" Then
        myValue = e.innerText
        MsgBox (myValue)
    End If
Next c

#1


1  

Try this:

试试这个:

For Each c In e.Cells
    If c.innerText Like "xtu_id" Then
        myValue = e.innerText
        MsgBox (myValue)
    End If
Next c