Скачать файл с веб-страницы с помощью Excel VBA - PullRequest
0 голосов
/ 01 декабря 2018

Я пытаюсь загрузить данные дождемера с веб-сайта с 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

1 Ответ

0 голосов
/ 01 декабря 2018

Я пытался держаться ближе к вашему исходному коду.Вот шаги, которые включают в себя пропущенный шаг отображения выпадающего меню, чтобы вы могли выбрать формат и т. Д.

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...