Import tables from a webpage to excel

喜夏-厌秋 提交于 2019-12-31 07:25:09

问题


I just have an upper-intermediate level of excel and intermediate background of VBA with excel. What I want to do is to import tables from a webpage like this which appears in the link: http://www.admision.unmsm.edu.pe/res20130914/A/011/0.html

That webpage shows 39 links and each link contains a table. So I would like to know an automatic way to import all these tables to excel.


回答1:


This code get the data from all the links.

Sub Extract_data()

    Dim url As String, links_count As Integer
    Dim i As Integer, j As Integer, row As Integer
    Dim XMLHTTP As Object, html As Object
    Dim tr_coll As Object, tr As Object
    Dim td_coll As Object, td As Object

    links_count = 39
    For i = 0 To links_count

        url = "http://www.admision.unmsm.edu.pe/res20130914/A/011/" & i & ".html"

        Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
        XMLHTTP.Open "GET", url, False
        XMLHTTP.send

        Set html = CreateObject("htmlfile")
        html.body.innerHTML = XMLHTTP.ResponseText

        Set tbl = html.getelementsbytagname("Table")

        Set tr_coll = tbl(0).getelementsbytagname("TR")

        For Each tr In tr_coll
            j = 1
            Set td_col = tr.getelementsbytagname("TD")

            For Each td In td_col
                Cells(row + 1, j).Value = td.innerText
                j = j + 1
            Next
            row = row + 1
        Next
    Next

    MsgBox "Done"
End Sub


来源:https://stackoverflow.com/questions/20832017/import-tables-from-a-webpage-to-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!