【问题标题】:Excel VBA code to fetch value/maltiple value from web siteExcel VBA代码从网站获取值/多个值
【发布时间】:2016-10-24 12:43:49
【问题描述】:

我正在尝试通过在 Excel 中使用 VBA 代码从 http://www.dsebd.org/displayCompany.php?name=LINDEBD 获取数据。

试图从此链接获取以下数据

已发行证券总数 部门 公司名称

正在尝试,

    Sub GetData()
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.dsebd.org/displayCompany.php?name=LINDEBD"

With IE
    .Visible = True
    .navigate my_url
    .Top = 50
    .Left = 530
    .Height = 400
    .Width = 400

Do Until Not IE.Busy And IE.readyState = 4
    DoEvents
Loop

End With

Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading

Set Results = IE.document.getElementsByTagName("table")
For Each itm In Results
    If itm.classname = "lgn" Then
        dd = itm.getAttribute("href")
        Exit For
    End If
    Next
' if you wnat to click the link
    itm.Click
' otherwise
    'Range("a1").Value = dd
    MsgBox dd
End Sub

是否可以从本网站收集数据

【问题讨论】:

  • 那里的代码出了什么问题?
  • 运行时错误 404,需要对象。我觉得这里有问题, If itm.classname = "lgn" Then dd = itm.getAttribute("href")
  • 注释掉itm.Click会停止错误,但dd最后是NULL,因为代码找不到任何classnamelgn

标签: vba excel


【解决方案1】:

首先,您必须迭代表(table),然后迭代行(tr),然后在其中迭代单元格(td)。这些不是此页面上的链接。

注意:以下将为您提供您所寻求的价值,但如果网站发生变化,此代码将不断变化。

Sub GetData()
    Set IE = CreateObject("InternetExplorer.Application")
    my_url = "http://www.dsebd.org/displayCompany.php?name=LINDEBD"

    With IE
        .Visible = True
        .navigate my_url
        .Top = 50
        .Left = 530
        .Height = 400
        .Width = 400

    Do Until Not IE.Busy And IE.readyState = 4
        DoEvents
    Loop

    End With

    Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading

    Set Results = IE.document.getElementsByTagName("table")
    For Each itm In Results
        Set rowAlt = IE.document.getElementsByClassName("alt")
        For Each itmTR In rowAlt
            Set tdValues = itmTR.getElementsByTagName("td")
            If tdValues.Item(0).innerText = "Total No. of Outstanding Securities" Then
                MsgBox tdValues.Item(1).innerText 'the value you seek
                Exit Sub 'don't continue this loop
            End If
        Next
    Next
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2014-08-17
    • 2019-11-28
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多