html - 使用 VBA 进行网页抓取 - 更改输入框的值

标签 html excel vba

我对 VBA 很有经验,但对网络抓取真的很陌生。到目前为止,我设法从其他网页中提取了一些表格,但是这个让我很难过。链接是 http://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?sector=6&accion=consultarCuadro&idCuadro=CF102&locale=es

基本上,我单击“Exportar Cuadro”按钮旁边的箭头下拉列表。之后,我需要将出现在那里的两个日期更改为我将在变量中拥有的特定日期。

如何更改网页上的输入框?到目前为止我的代码是下一个:

Option Explicit

Sub test()

Dim URL As String, URL2 As String, URL3 As String, URL4 As String
Dim IE As Object, obj As Object, colTR As Object, doc As Object, tr As Object
Dim eleColtr As MSHTML.IHTMLElementCollection 'Element collection for tr tags
Dim eleColtd As MSHTML.IHTMLElementCollection 'Element collection for td tags
Dim eleRow As MSHTML.IHTMLElement 'Row elements
Dim eleCol As MSHTML.IHTMLElement 'Column elements
Dim objCollection As Object
Dim j As String, i As Integer


URL = "https://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?sector=18&accion=consultarCuadroAnalitico&idCuadro=CA51&locale=es"
URL2 = "https://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?sector=18&accion=consultarCuadroAnalitico&idCuadro=CA52&locale=es"
URL3 = "https://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?sector=18&accion=consultarCuadroAnalitico&idCuadro=CA53&locale=es"
URL4 = "http://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?sector=6&accion=consultarCuadro&idCuadro=CF102&locale=es"
'Tipos de cambio
Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True
IE.navigate URL4

Do While IE.Busy Or IE.readyState <> 4
    DoEvents
Loop

Application.Wait (Now + TimeValue("00:00:01"))

IE.document.getElementById("exportaCuadroToggle").Click

Set objCollection = IE.document.getElementsByTagName("ID")
i = 0
While i < objCollection.Length
    If objCollection(i).Value = "26/08/2019" Then
        ' Set text for search
        objCollection(i).Value = "01/08/2019"
    End If
    If objCollection(i).Name = "form-control form-control-sm fechaFin" Then
        ' Set text for search
        objCollection(i).Value = "01/08/2019"
    End If
Wend

End Sub

注意:URLURL2URL3 用于完整代码,但我暂时省略了那部分,因为这些链接已经在做我想要什么。

最佳答案

据我所知,更改下拉框中的日期不会更新页面中显示的表格,这意味着没有必要抓取它。

除非我遗漏了什么,否则下载 excel 文件并使用 vba 对其进行操作以获取所需数据似乎要容易得多。因此我不会解决“如何更改输入框中的日期”问题,因为我发现它是徒劳的。相反,我会建议一种替代方法。

如果您使用浏览器的开发人员工具检查网络流量,您会看到当您按下“Exportar cuadro”按钮时,正在发送一个 GET 请求,该请求使用开始和unix 时间戳中的结束日期并返回相应的 excel 文件。您只需要 URL

这是一个如何获取文件的示例:

Option Explicit

Sub Test()

Dim wb As Workbook
Dim url As String
Dim startDate As Double
Dim endDate As Double
startDate = ToUnix("10/08/2019") 'use whichever date you want
endDate = ToUnix("20/08/2019") 'use whichever date you want
url = "http://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?sector=6&accion=consultarCuadro&idCuadro=CF102&locale=es&formatoXLS.x=1&fechaInicio=" & startDate & "&fechaFin=" & endDate
Set wb = Workbooks.Open(url)

End Sub

Public Function ToUnix(dt As Date) As Double 'credits to @Tim Williams
ToUnix = DateDiff("s", "1/1/1970", dt) * 1000
End Function

出于演示目的,上面的代码将只打开两个随机日期的报告。一旦工作簿存储在工作簿变量中,您就可以像往常一样操作它,并用它做任何你想做的事。

您可以修改代码以满足您的需要。

现在,话虽如此,website offers an API带有大量文档和示例,您可以使用它们以快速可靠的方式获取所需的任何信息。我强烈建议调查一下。

另外,没有名为“ID”的 HTML 标签这样的东西,因此:

IE.document.getElementsByTagName("ID")

应该返回Nothing

关于html - 使用 VBA 进行网页抓取 - 更改输入框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57666248/

相关文章:

HTML 到 Excel 格式 : mso-number-format currency without decimal?

excel - Excel 中的 VBA 引用表名称

html - 如何将元素固定在可滚动页面的中心?

javascript - 并排显示 div 向左浮动不起作用

vb.net - 如何使用 VB.NET 将 XLS 中的单个工作表转换为 PDF

vba - Excel VBA 循环遍历数据透视项

vba - 如何优化在大量记录上运行的宏?

vba - 将撤消构建到 Excel VBA 宏中

asp.net - 显示图像从一个 div 溢出到另一个 div

html - 使用 Xpath 抓取不一致的 DOM