如何从网页(带有“ div类”)中导入表格到Excel?

时间:2019-05-23 14:29:34

标签: excel vba web-scraping

我正在尝试从此webpage导入参展商和国家/地区列表,但没有得到。

有人可以帮我吗?

我已经尝试了该论坛中列出的方法,但不起作用。

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

对不起,我的英语。

1 个答案:

答案 0 :(得分:0)

您不需要打开浏览器。您可以使用XHR做到这一点。我正在使用的url可以通过 F12 (Dev工具)

在网络标签中找到

如果在发出请求后搜索该选项卡,则将找到该URL,并且响应具有如下布局:

图片链接:https://i.stack.imgur.com/C8oLj.png

我循环行和列以填充2d数组(类似于表格的格式),然后我将它一口气写到表中。


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