【问题标题】:Get a single value from a table with no ID with VBA使用 VBA 从没有 ID 的表中获取单个值
【发布时间】:2017-12-28 18:53:04
【问题描述】:

我正在开发一个从不同国家海关网站抓取进口税的网络机器人,我在从以下网站检索我想要的值时遇到问题:http://www.aduanet.gob.pe/itarancel/arancelS01Alias,使用 CODIGO 旁边的测试值 3303000000。我要检索的值是“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

【问题讨论】:

  • 要达到你所追求的内容,你必须用两个iframes来跨越。

标签: excel vba internet-explorer


【解决方案1】:

这是您从该页面获取数据的方式。需要从该页面切换两个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%

【讨论】:

  • 非常感谢!代码运行良好。我现在只是在阅读有关 iFrames 的信息,并且刚刚明白为什么我尝试过的任何方法都不起作用。
猜你喜欢
  • 2020-07-05
  • 1970-01-01
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2018-08-15
相关资源
最近更新 更多