EXCEL VBA网抓技巧-复制网页表格,不用遍历单元格
''''对应表格复制
Sub tableTest()Set winhttp = CreateObject("winhttp.WinHttpRequest.5.1")Set HTML = CreateObject("htmlfile")Set oWindow = HTML.ParentWindowUrl = "https://www.taiwanlottery.com.tw/Lotto/BINGOBINGO/drawing.aspx"With winhttp.Open "GET", Url, False.sendstrText = .responsetext
'' Debug.Print strTextEnd WithHTML.body.innerhtml = strTextSet tables = HTML.getElementsByClassName("tableFull")Set Table = tables(0)'''写入剪切板 第一种oWindow.ClipboardData.SetData "text", Table.outerHTML'''写入剪切板 第二种
' Set clipboard = New MSForms.DataObject
' clipboard.SetText Table.outerHTML
' clipboard.PutInClipboardActiveSheet.Range("a1").SelectActiveSheet.PasteSet winhttp = NothingSet HTML = NothingSet oWindow = Nothing
End Sub
''''所有表格
Sub alltableTest()Set winhttp = CreateObject("winhttp.WinHttpRequest.5.1")Set HTML = CreateObject("htmlfile")Set oWindow = HTML.ParentWindowUrl = "https://www.taiwanlottery.com.tw/Lotto/BINGOBINGO/drawing.aspx"With winhttp.Open "GET", Url, False.sendstrText = .responsetext
'' Debug.Print strTextEnd WithHTML.body.innerhtml = strTextSet tables = HTML.getElementsByTagName("table")aa = 1For i = 0 To tables.Length - 1Set Table = tables(i)'''写入剪切板 第一种oWindow.ClipboardData.SetData "text", Table.outerHTML'''写入剪切板 第二种' Set clipboard = New MSForms.DataObject' clipboard.SetText Table.outerHTML' clipboard.PutInClipboardActiveSheet.Cells(1, aa).SelectActiveSheet.PasteoWindow.ClipboardData.SetData "text", ""aa = ActiveSheet.UsedRange.Columns.Count + 2NextSet winhttp = NothingSet HTML = NothingSet oWindow = Nothing
End Sub