【问题标题】:how to insert a value in a html space box by vba如何通过vba在html空格框中插入一个值
【发布时间】:2019-05-07 20:25:26
【问题描述】:

我正在尝试在 3 个不同的空格框中插入一个特定的值。但是,我没有找到适用于我正在使用的网站的示例。

网站:http://www.b3.com.br/pt_br/market-data-e-indices/indices/indices-de-segmentos-e-setoriais/serie-historica-do-di.htm

Sub Download()

Dim user, password As Variant

Application.ScreenUpdating = False

'Planilha Ativa
Worksheets("Realizado").Activate

'Abrir a página da BMF
Set ie = CreateObject("InternetExplorer.Application")
    ie.navigate "http://www.b3.com.br/pt_br/market-data-e-indices/indices/indices-de-segmentos-e-setoriais/serie-historica-do-di.htm"
    ie.Visible = True

ie.Document.all.Item("DT_DIA_DE").Value = "02"
ie.Document.all.Item("DT_MES_DE").Value = "01"
ie.Document.all.Item("DT_ANO_DE").Value = "2019"

Application.ScreenUpdating = True


End Sub

【问题讨论】:

    标签: html excel vba internet-explorer web-scraping


    【解决方案1】:

    Internet Explorer:

    它在 iframe 中。您最初可以使用

    直接导航到 iframe 的 src 源
    .Navigate2 http://estatisticas.cetip.com.br/astec/series_v05/paginas/lum_web_v05_template_informacoes_di.asp?str_Modulo=completo&int_Idioma=1&int_Titulo=6&int_NivelBD=2
    

    或者,选择 iframe 元素并导航到其源代码

    Option Explicit
    'VBE > Tools > References: Microsoft Internet Controls
    Public Sub NavigateLinks()
        Dim ie As New InternetExplorer
        With ie
            .Visible = True
            .Navigate2 "http://www.b3.com.br/pt_br/market-data-e-indices/indices/indices-de-segmentos-e-setoriais/serie-historica-do-di.htm"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
            .Navigate2 .document.getElementById("bvmf_iframe").src
            While .Busy Or .readyState < 4: DoEvents: Wend
            With .document
                .querySelector("[name='DT_DIA_DE']").Value = "02"
                .querySelector("[name='DT_MES_DE']").Value = "01"
                .querySelector("[name='DT_ANO_DE']").Value = "2018"
                .querySelector("[href*='EnvioChecaDados']").Click
            End With
            Stop
            .Quit
        End With
    End Sub
    

    XHR (XMLHttpRequest):

    该页面向该 url 发出 POST 表单 XHR 请求:

    http://estatisticas.cetip.com.br/astec/series_v05/paginas/lum_web_v04_10_03_consulta.asp
    

    您可以尝试复制该请求并完全避免使用浏览器。如果您在页面上手动发出请求并通过开发工具(F12 - Chrome)检查网络选项卡,您将找到该 url。 POST 表单参数全部显示,包括日期部分:


    点击下载:

    .document.querySelector(".primary-text a").click
    

    【讨论】:

    • 谢谢@QHarr!最新求助:如果我使用第二个例子,需要下载(或者点击页面上方的按钮),我该怎么办?项目:“Clique aqui para fazer o download do arquivo .XLS”
    • 回答已接受,谢谢
    • 我不知道为什么,但是如果我正常运行代码(使用 F5),您建议的更新不起作用。我的意思是,如果我使用 F8 逐步查看代码,则代码有效,但前面的示例无效。并且完成下载的函数 Application.SendKeys "%{S}" 也不起作用。你有什么想法吗? QHarr
    • 你需要指出哪一行失败。如果与 f8 一起使用,则在失败的行之前暂停,例如 Application.Wait Now + TimeSerial(0,0,2)
    • 如果需要,将数字 2 增加到更高的秒数。
    猜你喜欢
    • 2014-02-03
    • 2020-02-06
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多