远程服务器计算机不存在或不可用”

时间:2020-05-10 06:40:34

标签: vba

Sub CountryPopList()
    ' declare the variables
    Dim ieObj As InternetExplorer
    Dim htmlEle As IHTMLElement
    Dim i As Integer

    ' initialize i to one
    i = 1

    ' create and get access to an instance of IE
    Set ieObj = New InternetExplorer
    ieObj.Visible = True
    ieObj.navigate "https://www.nseindia.com/companies-listing/corporate-filings-announcements"

    ' give the webpage some time to load all content
    Application.Wait Now + TimeValue("00:00:05")

    ' loop through all the rows in the table
    For Each htmlEle In ieObj.document.getElementsByClassName("table-CFanncEquity")(0).getElementsByTagName("tr")
        With ActiveSheet
            .Range("A" & i).Value = htmlEle.Children(0).textContent
            .Range("B" & i).Value = htmlEle.Children(1).textContent
            .Range("C" & i).Value = htmlEle.Children(2).textContent
            .Range("D" & i).Value = htmlEle.Children(3).textContent
            .Range("E" & i).Value = htmlEle.Children(4).textContent
        End With

        i = i + 1
    Next htmlEle

End Sub

1 个答案:

答案 0 :(得分:0)

您在需要getElementsByClassName()时正在使用getElementById() 这对我有用:

Sub CountryPopList()
    ' declare the variables
    Dim ieObj As InternetExplorer
    Dim htmlEle As IHTMLElement
    Dim i As Integer, tbl

    ' initialize i to one
    i = 1

    ' create and get access to an instance of IE
    Set ieObj = New InternetExplorer
    ieObj.Visible = True
    ieObj.navigate "https://www.nseindia.com/companies-listing/corporate-filings-announcements"

    ' give the webpage some time to load all content
    Application.Wait Now + TimeValue("00:00:05")

    ' loop through all the rows in the table
    Set tbl = ieObj.document.getElementById("table-CFanncEquity")
    For Each htmlEle In tbl.getElementsByTagName("tr")
        With ActiveSheet
            .Range("A" & i).Value = htmlEle.Children(0).textContent
            .Range("B" & i).Value = htmlEle.Children(1).textContent
            .Range("C" & i).Value = htmlEle.Children(2).textContent
            .Range("D" & i).Value = htmlEle.Children(3).textContent
            .Range("E" & i).Value = htmlEle.Children(4).textContent
        End With

        i = i + 1
    Next htmlEle

End Sub
相关问题