使用VBA从网站进行数据报道

时间:2017-08-28 08:06:43

标签: vba

我正在尝试从网站下载一些数据,点击后开始下载。 但是这段代码不起作用,任何人都可以提供帮助。

    Dim ie As InternetExplorer
    Dim htmldoc As MSHTML.IHTMLDocument
    Dim HTMLInput As MSHTML.IHTMLElementCollection
    Dim HTMLAs As MSHTML.IHTMLElementCollection
    Dim HTMLA As MSHTML.IHTMLElement

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

    'Navigate to webpage
    Dim ieURL As String:    ieURL = "http://erldc.org/final-schedule.aspx"
    ie.navigate ieURL
    Do While ie.readyState <> READYSTATE_COMPLETE
    Loop

    Set htmldoc = ie.document
    Set HTMLInput = htmldoc.getElementsByTagName("a")

    For Each HTMLA In HTMLAs
        Debug.Print HTMLA.getAttribute("classname"), HTMLA.getAttribute("href"), HTMLA.getAttribute("rel")

        If HTMLA.getAttribute("href") = "javascript:__doPostBack('ctl00$ContentPlaceHolder1$Calendar1','6420')" Then

            HTMLA.Click

            Exit For
        End If

    Next HTMLA

End Sub

1 个答案:

答案 0 :(得分:0)

尝试在检查Do While/Loop的{​​{1}}之后暂停宏几秒钟,也许是5秒......

ReadyState

另外,除了Dim sngFinish As Single Dim intPauseTime As Integer intPauseTime = 5 'in seconds sngFinish = timer + intPauseTime Do While timer < sngFinish DoEvents Loop 之外,我建议您检查Internet Explorer的Busy状态,然后添加ReadyState ...

DoEvents

希望这有帮助!