Excel VBA:从html表中获取特定的表数据

时间:2017-04-07 09:00:58

标签: vba excel-vba web-scraping excel

我正在尝试使用innerhtml从这个网站拉出主数据表然后循环但我得到一堆不同的表数据而且只是乱七八糟(尝试过webquerytable但我得到的格式不适合我) http://lcvpd.lt/lt/aktualijos/statistika/uzbsands.php

有没有办法只提取主数据表?

对不起沟通不好,我是vba的新手,不知道如何正确解释这个问题。

这是我到目前为止的地方:

Sub HTML_Table_To_Excel()

    Dim htm As Object
    Dim Tr As Object
    Dim Td As Object
    Dim Tab1 As Object

    'Replace the URL of the webpage that you want to download
    Web_URL = VBA.Trim(Sheets(1).Cells(1, 1))

    'Create HTMLFile Object
    Set HTML_Content = CreateObject("htmlfile")

    'Get the WebPage Content to HTMLFile Object
    With CreateObject("msxml2.xmlhttp")
        .Open "GET", Web_URL, False
        .send
        HTML_Content.body.innerHTML = .responseText
    End With

    Column_Num_To_Start = 1
    iRow = 2
    iCol = Column_Num_To_Start
    iTable = 10

    'Loop Through Each Table and Download it to Excel in Proper Format
    For Each Tab1 In HTML_Content.getElementsByTagName("table")
        With HTML_Content.getElementsByTagName("table")(iTable)
            For Each Tr In .Rows
                For Each Td In Tr.Cells
                    Sheets(1).Cells(iRow, iCol).Select
                    Sheets(1).Cells(iRow, iCol) = Td.innerText
                    iCol = iCol + 1
                Next Td
                iCol = Column_Num_To_Start
                iRow = iRow + 1
            Next Tr
        End With
        iTable = iTable + 1
        iCol = Column_Num_To_Start
        iRow = iRow + 1
    Next Tab1
End Sub

0 个答案:

没有答案