How to import a table from web page (with "div class") to excel?
Solution 1:
You don't need a browser to be opened. You can do this with XHR. The url I am using can be found in the network tab via F12 (Dev tools)
If you search that tab after making your request you will find that url and the response has a layout such as:
image link: https://i.stack.imgur.com/C8oLj.png
I loop the rows and the columns to populate a 2d array (table like format) which I write out to the sheet in one go at end.
VBA:
Option Explicit
Public Sub GetExhibitorsInfo()
Dim ws As Worksheet, results(), i As Long, html As HTMLDocument
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://sps.mesago.com/events/en/exhibitors_products/exhibitor-list.html", False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
html.body.innerHTML = .responseText
End With
Dim rows As Object, html2 As HTMLDocument, columnsInfo As Object
Dim r As Long, c As Long, j As Long, headers(), columnCount As Long
headers = Array("name2_kat", "art", "std_nr_sort", "kfzkz_kat", "halle", _
"sortierung_katalog", "std_nr", "ort_info_kat", "name3_kat", "webseite", _
"land_kat", "standbez1", "name1_kat")
Set rows = html.querySelectorAll("[data-entry]")
Set html2 = New HTMLDocument
html2.body.innerHTML = rows.item(0).innerHTML
columnCount = html2.querySelectorAll("[data-entry-key]").length
ReDim results(1 To rows.length, 1 To columnCount)
For i = 0 To rows.length - 1
r = r + 1: c = 1
html2.body.innerHTML = rows.item(i).innerHTML
Set columnsInfo = html2.querySelectorAll("[data-entry-key]")
For j = 0 To columnsInfo.length - 1
results(r, c) = columnsInfo.item(j).innerText 'columnsInfo.item(j).getAttribute("data-entry-key")
c = c + 1
Next
Next
With ws
.Cells(1, 1).Resize(1, columnCount) = headers
.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End With
End Sub