【问题标题】:Download file from webpage using Excel VBA使用 Excel VBA 从网页下载文件
【发布时间】:2018-12-01 19:33:50
【问题描述】:

我正在尝试使用 VBA 从网站下载雨量计数据。

我找到了用户输入定义的雨量计站号。搜索完成后,我的代码选中雨量站对应的复选框,数据格式不起作用。

当我手动执行此操作时,搜索完成后,我必须单击“Dados Convencionais”以显示搜索结果。我找不到在代码中执行此操作的方法。

Sub DownloadCSV()

Dim SearchString As String
Dim SearchBox As Object
Dim SearchButton As Object
Dim SelectionStationButton As Object
Dim SelectionCSVButton As Object
Dim DownloadButton As Object
Dim ie As New InternetExplorer

'User inputs station number
SearchString = InputBox("Input rain gauge station number", "Download data HidroWeb", "Station number- e.g. 02044054")

With ie
    .Visible = True
    .Navigate "http://www.snirh.gov.br/hidroweb/publico/medicoes_historicas_abas.jsf"

    While ie.ReadyState <> 4
    DoEvents
    Wend

    'Station number to be searched
    Set SearchBox = .Document.getElementById("form:fsListaEstacoes:codigoEstacao")
    SearchBox.Value = SearchString

    'Search button click
    Set SearchButton = .Document.getElementById("form:fsListaEstacoes:bt")
    SearchButton.Click

    'select checkbox beside the station number
    Set SelectionStationButton = .Document.getElementById("form:fsListaEstacoes:fsListaEstacoesC:j_idt178:table:0:ckbSelecionada")
    SelectionStationButton.Click

    'Select data format -  Arquivo Excel(.CSV)
    Set SelectionCSVButton = .Document.getElementById("form:fsListaEstacoes:fsListaEstacoesC:radTipoArquivo:2")
    SelectionCSVButton.Click

    'click download button
    Set DownloadButton = .Document.getElementById("form:fsListaEstacoes:fsListaEstacoesC:btBaixar")
    DownloadButton.Click

End With

End Sub

【问题讨论】:

  • 顺便说一句好帖子。希望所有新的贡献者都表现出这么大的努力。 +

标签: html excel vba web-scraping


【解决方案1】:

我已尝试与您的原始代码保持一致。以下步骤包括使下拉菜单出现的缺失步骤,以便您可以选择格式等。

Option Explicit

Sub DownloadCSV()

    Dim SearchString As String
    Dim SearchBox As Object
    Dim SearchButton As Object
    Dim SelectionStationButton As Object
    Dim SelectionCSVButton As Object
    Dim DownloadButton As Object
    Dim ie As New InternetExplorer

    'User inputs station number
    SearchString = "02044054"                    'InputBox("Input rain gauge station number", "Download data HidroWeb", "Station number- e.g. 02044054")

    With ie
        .Visible = True
        .Navigate2 "http://www.snirh.gov.br/hidroweb/publico/medicoes_historicas_abas.jsf"

        While .Busy Or .readyState < 4: DoEvents: Wend

        'Station number to be searched
        Set SearchBox = .document.getElementById("form:fsListaEstacoes:codigoEstacao")
        SearchBox.Value = SearchString

        'Search button click
        Set SearchButton = .document.getElementById("form:fsListaEstacoes:bt")
        SearchButton.Click

        'click dropdown
        .document.querySelector("[href*=dadosConvencionais]").Click

        'select checkbox beside the station number
        .document.querySelector(".checkbox.i-checks i").Click

        'Select data format -  Arquivo Excel(.CSV)
        .document.querySelector("input[value='3']").Click

        'click download button
        .document.querySelector("[id='form:fsListaEstacoes:fsListaEstacoesC:btBaixar']").Click

        Application.Wait Now + TimeSerial(0, 0, 10)
        .Quit
    End With

End Sub

【讨论】:

  • 感谢您的回复@QHarr!我运行了您的代码,但在此行收到一条消息“错误 424 - 需要对象” - .Document.querySelector("[href*=dadosConvencionais]").Click。我尝试修改它,但我无法使它正确。
  • 您可能需要等待。试试看,让我知道。跑了几次都ok。
猜你喜欢
  • 2017-01-30
  • 2015-12-07
  • 2013-06-17
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 2017-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多