【问题标题】:How to scrape table data in Excel/VBA?如何在 Excel/VBA 中抓取表格数据?
【发布时间】:2019-01-09 12:37:17
【问题描述】:

页面编码在html5,我的代码不能用这种方法刮:

Sub Macro1()
 With ActiveSheet.QueryTables.Add(Connection:= _
 “URL;https://exchange.btcc.com/”, Destination:=Range(“$A$1″))
 .Name = “market_trend”
 .FieldNames = True
 .RowNumbers = False
 .FillAdjacentFormulas = False
 .PreserveFormatting = True
 .RefreshOnFileOpen = False
 .BackgroundQuery = True
 .RefreshStyle = xlInsertDeleteCells
 .SavePassword = False
 .SaveData = True
 .AdjustColumnWidth = True
 .RefreshPeriod = 0
 .WebSelectionType = xlSpecifiedTables
 .WebFormatting = xlWebFormattingNone
 .WebTables = “4″
 .WebPreFormattedTextToColumns = True
 .WebConsecutiveDelimitersAsOne = True
 .WebSingleBlockTextImport = False
 .WebDisableDateRecognition = False
 .WebDisableRedirections = False
 .Refresh BackgroundQuery:=False
 End With`
 End Sub

知道如何从这个网站上抓取 Price Ticker 表吗?

【问题讨论】:

    标签: vba excel internet-explorer


    【解决方案1】:

    为什么不使用与上一个问题相同的方法:

    Sub Tester()
    
        Dim IE As Object
        Dim tbl, trs, tr, tds, td, r, c
    
        Set IE = CreateObject("internetexplorer.application")
    
        IE.Navigate "https://exchange.btcc.com/"
    
        Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
    
        Set tbl = IE.Document.getElementsByTagName("table")(5)
        Set trs = tbl.getElementsByTagName("tr")
    
        For r = 0 To trs.Length - 1
            Set tds = trs(r).getElementsByTagName("td")
            If tds.Length = 0 Then Set tds = trs(r).getElementsByTagName("th")
    
            For c = 0 To tds.Length - 1
                ActiveSheet.Range("A1").Offset(r, c).Value = tds(c).innerText
            Next c
        Next r
    
    End Sub
    

    这将为您提供如下结果:

    【讨论】:

    • @R.Michael - 我得到了正确的结果,检查你的单元格范围的格式。
    • 我做了新工作表,添加仍然无法正常工作。你说的单元格范围是什么意思
    • @R.Michael - 我所说的单元格范围是指您的数据被复制的范围。
    • @R.Michael - 您能否分享您的工作表以便我进行调查。
    • 和你的一样 Mrig :/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2018-03-07
    • 1970-01-01
    • 2023-03-13
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多