html - 如何通过vba在html空格框中插入值

标签 html excel vba internet-explorer web-scraping

我正在尝试在 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

最佳答案

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 元素并导航到其 src

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)检查网络选项卡,您将找到该网址。 POST 表单参数全部显示,包括日期部分:

enter image description here


点击下载:

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

关于html - 如何通过vba在html空格框中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030005/

相关文章:

excel - 遍历数据透视表并将每个字段的详细信息保存在具有字段名称的单独工作簿中

javascript - jQuery 函数未启动

Javascript 循环遍历对象以填充 HTML 选择

html - 悬停时如何用填充突出显示整个表格单元格?

excel - Power Query List.RemoveNulls 列集合

c# - 如何检查Excel工作簿或工作表是否受密码保护?

python : running Excel macro with python

arrays - 根据多个条件返回其他工作表中的匹配值

vba - Excel VBA : how to access and control the "Sheet Select" dialog?

html - 如何在iframe中插入html