0

I'm trying to import to Excel a list of exhibitors and countries from this webpage and I'm not getting it.

Can Someone help me?

I have tried the methods listed in this forum and doesn't work.

Sub test()

    Dim objIE As Object
    Dim hmtl As HTMLDocument

    Dim elements As IHTMLElementCollection

    Set objIE = New InternetExplorer
    objIE.Visible = True

    objIE.navigate "https://sps.mesago.com/events/en/exhibitors_products/exhibitor-list.html"

    Application.StatusBar = "Loading, Please wait..."

    While objIE.Busy
        DoEvents
    Wend
    Do
    Loop Until objIE.readyState = READYSTATE_COMPLETE

    Application.StatusBar = "Importing data..."

    Set html = objIE.document

    'I try differents types and name - ByClassName("..."), ByTagName("..."), ...
    Set elements = html.getElementsByClassName("list") 

    For i = 0 To elements.Length - 1
         Sheet1.Range("A" & (i + 1)) = elements(i).innerText
    Next i

    objIE.Quit
    Set objIE = Nothing

    Application.StatusBar = ""

End Sub

Sorry about my English.

Robert Todar
  • 2,085
  • 2
  • 11
  • 31
RuiDias
  • 1
  • 1
  • 1
  • 1
    You might benefit from reading [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/a/284237/3219613) • Also *"does not work"* is no useful error description. Instead you need to describe what error you get. – Pᴇʜ May 23 '19 at 14:32

1 Answers1

4

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
QHarr
  • 83,427
  • 12
  • 54
  • 101