Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用excel vba分析HTML时出错_Html_Vba_Excel - Fatal编程技术网

使用excel vba分析HTML时出错

使用excel vba分析HTML时出错,html,vba,excel,Html,Vba,Excel,因此,由于限制,我需要用excel vba解析一些难看的html。HTML的问题是它没有元素ID。我有一个页面,其中有许多未标记的表,每个表都有几行。我唯一能从中构建的是,我需要提取的一个单元格中有一个标识符。每次ID“xtu_ID”作为值出现在表的某一行的单元格中时,我都希望从该行提取数据。看起来是这样的: <tr> <td> col1 </td> <td>

因此,由于限制,我需要用excel vba解析一些难看的html。HTML的问题是它没有元素ID。我有一个页面,其中有许多未标记的表,每个表都有几行。我唯一能从中构建的是,我需要提取的一个单元格中有一个标识符。每次ID“xtu_ID”作为值出现在表的某一行的单元格中时,我都希望从该行提取数据。看起来是这样的:

<tr>



<td>

                    col1

</td>


<td>

                    col2

</td>


<td>

                    xtu_id

</td>


<td>

                    col4

</td>


</tr>  

可乐
可乐
xtu_id
可乐
现在我看到这一行中存在xtu_id,我想将该行的所有单元格转储到excel工作表中。以下是我在阅读其他stackoverflow帖子时使用的内容:

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
子命令按钮1\u单击()
Dim应用程序作为InternetExplorerMedium
Set appIE=New InternetExplorerMedium
和阿皮
.导航“https://my_website"
.Visible=True
以
在appIE.Busy或appIE.ReadyState 4时执行
多芬特
环
设置mydata=appIE.Document.getElementsByTagName(“tr”)
对于mydata中的每个e
对于e中的每个c
如果c.Cells().innerText像“xtu_id”,那么
myValue=c.Cells().innerText
MsgBox(myValue)
如果结束
下一个c
下一个e
设置appIE=Nothing
端接头
这段代码一直有效,直到我到达[for each…]语句,我很难遍历每行的每个单元格来搜索“xtu_id”文本。有什么办法吗?

试试这个:

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

感谢Scott的快速回复,我在中交换了它,没有得到值,但是代码没有出错。也许我的if语句不是真的,你是否逐行进行调试?我之前提供的一个可能会有所帮助。感谢Scott的帮助,所以我将e.innertext替换为c.innertext来提取单元格值,并得到了我想要的结果。你的回答是正确的,这是我的错,因为我不清楚我想拉什么绳子。再次感谢!