从没有ID的表中获取VBA

时间:2017-12-27 20:32:19

标签: excel vba internet-explorer

我正在开发一个从不同国家海关网站上扣除进口税的网络机器人,我在使用CODIGO旁边的测试值3303000000从以下网站检索我想要的值时遇到问题:http://www.aduanet.gob.pe/itarancel/arancelS01Alias。我想要检索的值是" Ad / Valorem"旁边的6%,但是它所在的表没有ID属性,也没有类或与直接相关或至少接近它的相关内容。我一直在尝试使用.parent和.child方法,但没有成功。到目前为止,我的代码如下:

Function Peru(partida As String) As String

'Open IE
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "http://www.aduanet.gob.pe/itarancel/arancelS01Alias"

'Load sub
Cargar

'Navigate further into the website (Im using partida = 3303000000)
For Each box In objIE.document.getElementsByTagName("input")
    If box.Name = "cod_partida" Then
        box.Value = partida
        Exit For
    End If
Next

For Each boton In objIE.document.getElementsByTagName("input")
    If boton.Value = "Consultar" Then
        boton.Click
        Exit For
    End If
Next

'Get the 6% value (This part is the one I cant figure out)

End Function

1 个答案:

答案 0 :(得分:0)

这是您从该页面获取数据的方法。需要从该页面切换两个iframes以达到所需内容。

Sub Aduanet_Info()
    Dim IE As New InternetExplorer, html As HTMLDocument
    Dim elem As Object, frm As Object, frm1 As Object

    With IE
        .Visible = False
        .navigate "http://www.aduanet.gob.pe/itarancel/arancelS01Alias"
        Do While .readyState <> READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    html.getElementsByTagName("input")(0).Value = "3303000000"
    html.getElementsByTagName("input")(3).Click
    Application.Wait Now + TimeValue("00:00:05")

    Set frm = html.getElementsByClassName("autoHeight")(0).contentWindow.document
    Set frm1 = frm.getElementsByClassName("autoHeight")(1).contentWindow.document

    For Each elem In frm1.getElementsByTagName("td")
        If InStr(elem.innerText, "Valorem") > 0 Then MsgBox elem.NextSibling.NextSibling.innerText: Exit For
    Next elem

    IE.Quit
End Sub

输出:

6%