vba - 点击链接后如何导航回来?

标签 vba excel internet-explorer web-scraping click

我对在 vba 中使用 IE 还很陌生,所以有时我很难纠正在编写从网络上抓取数据的代码时所犯的任何错误。我编写了一些代码,用于单击首页标题 Microsoft 计算机培训视频 下的 20 个链接中的每个视频链接。我希望单击每个链接,然后返回并重复该过程,直到单击所有链接。我在抓取工具中定义的类名称和标签名称是准确的。我需要做的就是以正确的方式执行该过程。此时,我的抓取工具正在单击 20 个链接中的最后一个链接,并卡在它到达的地方,但不导航回来。

这就是我到目前为止所写的内容。

Sub clicking_links()

    Const surl As String = "http://www.wiseowl.co.uk/videos/"
    Dim IE As New InternetExplorer, iedoc As HTMLDocument
    Dim posts As Object

    With IE
        .Visible = True
        .navigate surl
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set iedoc = IE.document
    End With

    For Each posts In iedoc.getElementsByClassName("woVideoListDefaultSeriesTitle")
        posts.getElementsByTagName("a")(0).Click
    Next posts

End Sub

最佳答案

尝试以下操作:

Sub clicking_links()

    Const surl As String = "http://www.wiseowl.co.uk/videos/"
    Dim newurl as String
    Dim IE As New InternetExplorer, iedoc As HTMLDocument
    Dim posts As Object
    Dim t As Long, i As Long

    With IE
        .Visible = True
        .navigate surl
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set iedoc = .document

        For Each posts In iedoc.getElementsByClassName("woVideoListDefaultSeriesTitle")
            t = t + 1 'count the number of posts
        Next posts

        For i = 1 To t
            Debug.Print i
            newurl = iedoc.getElementsByClassName("woVideoListDefaultSeriesTitle")(i - 1).getElementsByTagName("a")(0).href
            Debug.Print newurl

            .navigate newurl
            Do Until .readyState = READYSTATE_COMPLETE: Loop
            Set iedoc = .document
            'here do your stuff within the new url

            .navigate surl 'back to old url
            Do Until .readyState = READYSTATE_COMPLETE: Loop
            Set iedoc = .document
        Next i

    End With
End Sub

关于vba - 点击链接后如何导航回来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46717876/

相关文章:

javascript - 如何使用javascript或其他方式清除IE DNS缓存?

html - 背景不在ie8中

vba - 使用反向索引+匹配向上查找值

vba - 查找已填充任何颜色的所有单元格并突出显示 Excel VBA 中相应的列标题

vba - 如何使录制的宏在任何工作表上工作?

html - Internet Explorer 对齐问题

json - 从 MS Access 解析 VBA 中的 JSON (US BLS),更新

excel - 计算筛选工作表中的可见行数

由于内存碎片,Excel 2013 崩溃

python - 通过python3将带有制表符分隔的.txt文件转换为xlsx