首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Excel从网页下载文件

使用Excel从网页下载文件
EN

Stack Overflow用户
提问于 2018-12-01 19:33:50
回答 1查看 659关注 0票数 3

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

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

当我手动完成搜索后,我必须点击"Dados Convencionais“来显示搜索结果。我找不到用代码做这件事的方法。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-01 20:16:20

我试着接近你的原始代码。下面是一些步骤,其中包括一个缺失的步骤,使下拉列表出现,这样您就可以选择格式等。

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53574366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档