如何从网页(带有"div class")导入表格到Excel? [英] How to import a table from web page (with "div class") to excel?

查看:259
本文介绍了如何从网页(带有"div class")导入表格到Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此

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

有人可以帮我吗?

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

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

对不起,我的英语.

推荐答案

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

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)

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

If you search that tab after making your request you will find that url and the response has a layout such as:

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

我循环行和列以填充2d数组(类似于表格的格式),最后我一口气将其写到工作表中.

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

这篇关于如何从网页(带有"div class")导入表格到Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆