Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用Excel VBA从网页下载文件_Html_Excel_Vba_Web Scraping - Fatal编程技术网

Html 使用Excel VBA从网页下载文件

Html 使用Excel VBA从网页下载文件,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,我正试图用VBA从一个网站下载雨量计数据 我找到了由用户输入定义的雨量计站号。搜索完成后,我的代码将选中对应于雨量计站的复选框,并且数据格式不起作用 当我手动执行此操作时,搜索完成后,我必须单击“Dados Convencionais”以显示搜索结果。我找不到一种用代码实现的方法 Sub DownloadCSV() Dim SearchString As String Dim SearchBox As Object Dim SearchButton As Object Dim Selectio

我正试图用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
Sub-DownloadCSV()
将搜索字符串设置为字符串
将搜索框变暗为对象
将搜索按钮变暗为对象
Dim SelectionStationButton作为对象
Dim SELECTIONCSV按钮作为对象
Dim DownloadButton作为对象
Dim ie成为新的InternetExplorer
'用户输入站号
SearchString=InputBox(“输入雨量计站号”、“下载数据HidroWeb”、“站号-例如02044054”)
与ie
.Visible=True
.导航“http://www.snirh.gov.br/hidroweb/publico/medicoes_historicas_abas.jsf"
而ie.ReadyState 4
多芬特
温德
'要搜索的站点号
Set SearchBox=.Document.getElementById(“表单:fsListaEstacoes:codigoEstacao”)
SearchBox.Value=SearchString
'搜索按钮单击
设置SearchButton=.Document.getElementById(“表单:fsListaEstacoes:bt”)
搜索按钮。单击
'选择站号旁边的复选框
设置SelectionStationButton=.Document.getElementById(“表单:fsListaEstacoes:fsListaEstacoesC:j_idt178:table:0:ckbSelecionada”)
SelectionStation按钮。单击
'选择数据格式-Arquivo Excel(.CSV)
设置SelectionCSVButton=.Document.getElementById(“表单:fsListaEstacoes:fsListaEstacoesC:radTipoArquivo:2”)
选择CSV按钮。单击
'单击下载按钮
设置DownloadButton=.Document.getElementById(“表单:fsListaEstacoes:fsListaEstacoesC:btBaixar”)
下载按钮。单击
以
端接头

我试图保持与您的原始代码相近。以下是一些步骤,其中包括使下拉列表显示以便您可以选择格式等缺少的步骤

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
选项显式
子下载CSV()
将搜索字符串设置为字符串
将搜索框变暗为对象
将搜索按钮变暗为对象
Dim SelectionStationButton作为对象
Dim SELECTIONCSV按钮作为对象
Dim DownloadButton作为对象
Dim ie成为新的InternetExplorer
'用户输入站号
SearchString=“02044054”输入框(“输入雨量计站号”、“下载数据HidroWeb”、“站号-例如02044054”)
与ie
.Visible=True
.导航2“http://www.snirh.gov.br/hidroweb/publico/medicoes_historicas_abas.jsf"
当.Busy或.readyState<4:DoEvents:Wend时
'要搜索的站点号
Set SearchBox=.document.getElementById(“表单:fsListaEstacoes:codigoEstacao”)
SearchBox.Value=SearchString
'搜索按钮单击
设置SearchButton=.document.getElementById(“表单:fsListaEstacoes:bt”)
搜索按钮。单击
'单击下拉列表
.document.querySelector(“[href*=DadosconVencios]”)。单击
'选择站号旁边的复选框
.document.querySelector(“.checkbox.i-checks i”)。单击
'选择数据格式-Arquivo Excel(.CSV)
.document.querySelector(“输入[value='3']”)。单击
'单击下载按钮
.document.querySelector(“[id='form:fslisteastacoes:fslisteastacoesc:btBaixar']”)。单击
应用程序。立即等待+时间序列(0,0,10)
退出
以
端接头

顺便说一句,这篇文章写得不错。希望所有新的投稿人都能表现出这么大的努力+感谢您的回复@QHarr!我运行了您的代码,但在这一行-.Document.querySelector(“[href*=dadosConvencionais]”处收到一条消息“Error 424-需要对象”。单击。我试着修改它,但我无法使它正确。您可能需要等待一段时间。试试看,让我知道。我跑了好几次。