【问题标题】:VBA code to copy table from webpage into Excel将表格从网页复制到 Excel 的 VBA 代码
【发布时间】:2019-04-19 15:23:19
【问题描述】:

我已经修改了代码以尝试获取一系列相似的表。但是,复制到各个工作表中的这些表完全相同,即第一个变量/工作表的表已复制到为不同变量创建的其他工作表 - 不同工作表上的表应该不同。我的新代码有什么问题?您的再次建议将不胜感激!

Sub CopyWebTable()

    Dim IE As InternetExplorer, hTable As Object, clipboard As Object, t As Date
    Dim Var As String
    Const MAX_WAIT_SEC As Long = 5

    For i = 1 To 3
        Var = ThisWorkbook.Worksheets("Par").Range("B" & i + 2)

        Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        Set IE = New InternetExplorer

        With IE
            .Visible = True
            .Navigate2 "https://eresearch.fidelity.com/eresearch/evaluate/fundamentals/earnings.jhtml?tab=details&symbols=" & Var

            While .Busy Or .readyState < 4: DoEvents: Wend

            t = Timer                            'timed loop for details table to be present
            Do
                On Error Resume Next
                Set hTable = IE.document.querySelector(".earningsHistoryTable-Cont table")
                On Error GoTo 0
                If Timer - t > MAX_WAIT_SEC Then Exit Do
            Loop While hTable Is Nothing
            If Not hTable Is Nothing Then        'use clipboard to copy paste
                clipboard.SetText hTable.outerHTML
                clipboard.PutInClipboard
                ThisWorkbook.Worksheets(Var).Range("A1").PasteSpecial

            End If
        End With
    Next i

End Sub

【问题讨论】:

    标签: html excel vba web-scraping copy


    【解决方案1】:

    尝试以下构造,我们在 IE 对象的创建中将循环移到 vars 上,并确保在再次循环之前始终将 hTable 设置为空。

    Option Explicit
    
    Sub CopyWebTable()
    
        Dim IE As InternetExplorer, hTable As Object, clipboard As Object, t As Date
        Dim Var As String
        Const MAX_WAIT_SEC As Long = 5
        Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        Set IE = New InternetExplorer
    
        With IE
            .Visible = True
    
            For i = 1 To 3
                Var = ThisWorkbook.Worksheets("Par").Range("B" & i + 2)
    
                .Navigate2 "https://eresearch.fidelity.com/eresearch/evaluate/fundamentals/earnings.jhtml?tab=details&symbols=" & Var
    
                While .Busy Or .readyState < 4: DoEvents: Wend
    
                t = Timer                            'timed loop for details table to be present
                Do
                    On Error Resume Next
                    Set hTable = .document.querySelector(".earningsHistoryTable-Cont table")
                    On Error GoTo 0
                    If Timer - t > MAX_WAIT_SEC Then Exit Do
                Loop While hTable Is Nothing
                If Not hTable Is Nothing Then        'use clipboard to copy paste
                    clipboard.SetText hTable.outerHTML
                    clipboard.PutInClipboard
                    ThisWorkbook.Worksheets(Var).Range("A1").PasteSpecial
                    Set hTable = Nothing
                End If
            Next i
        End With
    End Sub
    

    【讨论】:

    • 再次感谢您的帮助,QHarr。您的代码在稍作改动后工作。
    • 看看能不能通过xmlhttp授权? codingislove.com/http-requests-excel-vba
    • 复制更多网页时发现代码运行缓慢。甚至不比手动复制和粘贴快。关于如何让它运行得更快的任何建议?
    • 主要问题是我无法正确测试这个,并且不确定其他人,如果没有适当的帐户,也可以。查看 XHR 以查看是否有办法通过 xmlhttp 获取信息,而不是打开速度较慢的浏览器。另外,查看公司是否提供您可以使用的 API(首选)。否则,不使用剪贴板,而是将表格存储在数组中并将数组写入工作表,可能会获得小幅收益。
    • 我尝试按照您的建议通过 xmlhttp 传递授权。我阅读了您发送的网页并将一些代码放在:stackoverflow.com/questions/55786109/… 但是,此代码不起作用。你能看看吗?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多