本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何使用 VBA 从网络下载表格?

发布于2025-01-19 22:40     阅读(466)     评论(0)     点赞(18)     收藏(2)


我正在尝试从此页面下载一个表格,然后使用 VBA 将其导入 Excel:http://www.merval.sba.com.ar/Vistas/Cotizaciones/Acciones.aspx --> 表格“Panel General”,我可以下载表格“Panel Merval”,但是无法下载其他表格。

我对表“Panel Merval”使用此代码:

Sub GetTable()

Dim ieApp As InternetExplorer
 Dim ieDoc As Object
 Dim ieTable As Object
 Dim clip As DataObject

'create a new instance of ie
 Set ieApp = New InternetExplorer

'you don’t need this, but it’s good for debugging
 ieApp.Visible = False

'now that we’re in, go to the page we want
 ieApp.Navigate "http://www.merval.sba.com.ar/Vistas/Cotizaciones/Acciones.aspx"
 Do While ieApp.Busy: DoEvents: Loop
 Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'get the table based on the table’s id
 Set ieDoc = ieApp.Document
 Set ieTable = ieDoc.all.Item("ctl00_ContentCentral_tcAcciones_tpMerval_grdMerval")

'copy the tables html to the clipboard and paste to teh sheet
 If Not ieTable Is Nothing Then
 Set clip = New DataObject
 clip.SetText "" & ieTable.outerHTML & ""
 clip.PutInClipboard
 Sheet1.Select
 Sheet1.Range("b2").Select
 Sheet1.PasteSpecial "Unicode Text"
 End If

'close 'er up
 ieApp.Quit
 Set ieApp = Nothing


End Sub

或者这个

Public Sub PanelLider()


Dim oDom As Object: Set oDom = CreateObject("htmlFile")
Dim x As Long, y As Long
Dim oRow As Object, oCell As Object
Dim vData As Variant
Dim link As String

link = "http://www.merval.sba.com.ar/Vistas/Cotizaciones/Acciones.aspx"

y = 1: x = 1

With CreateObject("msxml2.xmlhttp")
    .Open "GET", link, False
    .Send
    oDom.body.innerHTML = .ResponseText
End With

With oDom.getElementsByTagName("table")(27)
    Dim dataObj As Object
    Set dataObj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    dataObj.SetText "<table>" & .innerHTML & "</table>"
    dataObj.PutInClipboard
End With
Sheets(2).Paste Sheets(2).Cells(1, 1)


End Sub

有人可以帮我下载“Panel General”表格吗?

非常感谢。


解决方案


下面使用selenium basic获取表。

Option Explicit
Public Sub GetTable()
    Dim html As New HTMLDocument, htable As HTMLTable, headers()
    headers = Array("Especie", "Hora Cotización", "Cierre Anterior", "Precio Apertura", "Precio Máximo", _
"Precio Mínimo", "Último Precio", "Variación Diaria", "Volumen Efectivo ($)", "Volumen Nominal", "Precio Prom. Pon")
    With New ChromeDriver
        .get "http://www.merval.sba.com.ar/Vistas/Cotizaciones/Acciones.aspx"
        .FindElementById("__tab_ctl00_ContentCentral_tcAcciones_tpGeneral").Click
        Do
        DoEvents
        Loop While .FindElementById("ctl00_ContentCentral_tcAcciones_tpGeneral_dgrGeneral", timeout:=7000).Text = vbNullString
        html.body.innerHTML = .PageSource
        Set htable = html.getElementById("ctl00_ContentCentral_tcAcciones_tpGeneral_dgrGeneral")
        WriteTable2 htable, headers, 1, ActiveSheet
        .Quit
    End With
End Sub

Public Sub WriteTable2(ByVal htable As HTMLTable, ByRef headers As Variant, Optional ByVal startRow As Long = 1, Optional ByVal ws As Worksheet)
    If ws Is Nothing Then Set ws = ActiveSheet

    Dim tRow As Object, tCell As Object, tr As Object, td As Object, R As Long, c As Long, tBody As Object
    R = startRow: c = 1
    With ActiveSheet
        Set tRow = htable.getElementsByTagName("tr")
        For Each tr In tRow
            Set tCell = tr.getElementsByTagName("td")
            For Each td In tCell
                .Cells(R, c).Value = td.innerText
                c = c + 1
            Next td
            R = R + 1:  c = 1
        Next tr
        .Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
    End With
End Sub

参考:

  1. HTML 对象库
  2. Selenium 类型库

使用 IE (使用上面的 WriteTable2 子程序):

Option Explicit
Public Sub GetInfo()
    Dim ie As New InternetExplorer, html As HTMLDocument, hTable As HTMLTable, headers(), a As Object
    headers = Array("Especie", "Hora Cotización", "Cierre Anterior", "Precio Apertura", "Precio Máximo", _
"Precio Mínimo", "Último Precio", "Variación Diaria", "Volumen Efectivo ($)", "Volumen Nominal", "Precio Prom. Pon")
    Application.ScreenUpdating = False
    With ie
        .Visible = True
        .navigate "http://www.merval.sba.com.ar/Vistas/Cotizaciones/Acciones.aspx"
        While .Busy Or .readyState < 4: DoEvents: Wend
        .document.getElementById("__tab_ctl00_ContentCentral_tcAcciones_tpGeneral").Click
        Do
        DoEvents
        On Error Resume Next
        Set hTable = .document.getElementById("ctl00_ContentCentral_tcAcciones_tpGeneral_dgrGeneral")
        On Error GoTo 0
        Loop While hTable Is Nothing

        WriteTable2 hTable, headers, 1, ActiveSheet
        .Quit '<== Remember to quit application
        Application.ScreenUpdating = True
    End With
End Sub

参考:

  1. Microsoft Internet Explorer 控件



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.qianduanheidong.com/blog/article/538967/13c96141c841b4e46dc0/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

18 0
收藏该文
已收藏

评论内容:(最多支持255个字符)