EXCEL VBA网抓技巧-复制网页表格,不用遍历单元格

发布时间:2024年01月24日

EXCEL VBA网抓技巧-复制网页表格,不用遍历单元格

''''对应表格复制
Sub tableTest()
    Set winhttp = CreateObject("winhttp.WinHttpRequest.5.1")
    Set HTML = CreateObject("htmlfile")
    Set oWindow = HTML.ParentWindow
    Url = "https://www.taiwanlottery.com.tw/Lotto/BINGOBINGO/drawing.aspx"
    With winhttp
        .Open "GET", Url, False
        .send
        strText = .responsetext
''            Debug.Print strText
    End With
    HTML.body.innerhtml = strText
    Set tables = HTML.getElementsByClassName("tableFull")
    Set Table = tables(0)
    
    '''写入剪切板 第一种
    oWindow.ClipboardData.SetData "text", Table.outerHTML
    
    '''写入剪切板 第二种
'    Set clipboard = New MSForms.DataObject
'    clipboard.SetText Table.outerHTML
'    clipboard.PutInClipboard

    ActiveSheet.Range("a1").Select
    ActiveSheet.Paste
    
    Set winhttp = Nothing
    Set HTML = Nothing
    Set oWindow = Nothing
End Sub
''''所有表格
Sub alltableTest()
    Set winhttp = CreateObject("winhttp.WinHttpRequest.5.1")
    Set HTML = CreateObject("htmlfile")
    Set oWindow = HTML.ParentWindow
    Url = "https://www.taiwanlottery.com.tw/Lotto/BINGOBINGO/drawing.aspx"
    With winhttp
        .Open "GET", Url, False
        .send
        strText = .responsetext
''            Debug.Print strText
    End With
    HTML.body.innerhtml = strText
    Set tables = HTML.getElementsByTagName("table")
    aa = 1
    For i = 0 To tables.Length - 1
        Set Table = tables(i)
        
        '''写入剪切板 第一种
        oWindow.ClipboardData.SetData "text", Table.outerHTML
        
        '''写入剪切板 第二种
    '    Set clipboard = New MSForms.DataObject
    '    clipboard.SetText Table.outerHTML
    '    clipboard.PutInClipboard
    
        ActiveSheet.Cells(1, aa).Select
        ActiveSheet.Paste
        oWindow.ClipboardData.SetData "text", ""
        aa = ActiveSheet.UsedRange.Columns.Count + 2
    Next
    Set winhttp = Nothing
    Set HTML = Nothing
    Set oWindow = Nothing
End Sub

文章来源:https://blog.csdn.net/weixin_43050480/article/details/135832542
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。