Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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 如何使用VBA从web下载表格?_Html_Excel_Vba_Web Scraping - Fatal编程技术网

Html 如何使用VBA从web下载表格?

Html 如何使用VBA从web下载表格?,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,我正试图从这个页面下载一个表 使用VBA实现excel:-->表格“面板常规” 我可以下载“Panel Merval”表,但无法下载其他表 我将此代码用于表“Panel Merval”: 还是这个 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 vDat

我正试图从这个页面下载一个表 使用VBA实现excel:-->表格“面板常规” 我可以下载“Panel Merval”表,但无法下载其他表

我将此代码用于表“Panel Merval”:

还是这个

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
公共子面板()
Dim oDom作为对象:设置oDom=CreateObject(“htmlFile”)
尺寸x和长度一样,y和长度一样
Dim oRow作为对象,oCell作为对象
Dim vData作为变体
将链接设置为字符串
链接=”http://www.merval.sba.com.ar/Vistas/Cotizaciones/Acciones.aspx"
y=1:x=1
使用CreateObject(“msxml2.xmlhttp”)
.打开“获取”,链接,错误
.发送
oDom.body.innerHTML=.ResponseText
以
使用oDom.getElementsByTagName(“表”)(27)
Dim dataObj作为对象
Set dataObj=CreateObject(“新建:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}”)
dataObj.SetText“&.innerHTML&”
dataObj.PutInClipboard
以
纸张(2).粘贴纸张(2).单元格(1,1)
端接头
有人能帮我下载“Panel General”表格吗

非常感谢。

Selenium 下面使用获取表


参考文献:

  • HTML对象库
  • 硒类型库

  • 使用IE(使用上面的WriteTable2 sub):
    选项显式
    公共子GetInfo()
    Dim ie作为新InternetExplorer,html作为HTMLDocument,HTTable作为HTMLTable,headers(),a作为对象
    headers=数组(“specie”、“Hora Cotización”、“Cierre front”、“Precio Apertura”、“Precio Máximo”_
    “Precio Mínimo”、“Último Precio”、“Variación Diaria”、“有效体积($)”、“标称体积”、“Precio Prom.Pon”)
    Application.ScreenUpdating=False
    与ie
    .Visible=True
    .导航“http://www.merval.sba.com.ar/Vistas/Cotizaciones/Acciones.aspx"
    当.Busy或.readyState<4:DoEvents:Wend时
    .document.getElementById(“\uu选项卡\uCTL00\uContentCentral\uTCacciones\uTPGeneral”)。单击
    做
    多芬特
    出错时继续下一步
    Set hTable=.document.getElementById(“ctl00\u ContentCentral\u tcAcciones\u tpGeneral\u dRGeneral”)
    错误转到0
    循环,而hTable什么都不是
    WriteTable2 HTTable,标题,1,活动表
    .Quit'可能重复IMO(非专家)-这不是该问题的重复,因为这涉及到选项卡协商和处理事实,即基于ajax的加载。该问题中的方法只有在提出了正确的员额要求的情况下才能适用,但该问题没有足够的细节使使用该方法成为可能。
    
    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
    
    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
    
    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