【问题标题】:Can't populate results from a website using vba无法使用 vba 从网站填充结果
【发布时间】:2019-07-28 21:48:37
【问题描述】:

我使用 IE 在 vba 中创建了一个脚本,以便在从下拉列表中选择一个选项时填充内容。在网站的登录页面中有一个名为Type 的下拉菜单。当我点击Type 时,我可以在那里看到几个选项,我想点击其中的corporate bond。生成结果后,我想解析WKN

我尝试通过单击下拉列表并从那里选择一个选项来使用我当前的脚本填充结果。但是,它会单击下拉菜单并选择所需的选项,但无法在 InternetExplorer 中生成任何结果。

如何从下拉列表中选择一个选项来填充和解析结果?

到目前为止我的尝试:

Sub SelectItem()
    Const link = "https://www.boerse-stuttgart.de/en/tools/product-search/bonds/"
    Dim IE As New InternetExplorer, Html As HTMLDocument, post As Object, T As Date

    With IE
        .Visible = True
        .navigate link
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
    End With

    Application.Wait Now + TimeValue("00:00:15")
    Html.querySelector("#bsg-filters-btn-bgs-filter-3 .bsg-btn__icon").Click
    Application.Wait Now + TimeValue("00:00:05")
    Html.querySelector("#bsg-filters-menu-bgs-filter-3 .bsg-scrollbox label[for='bsg-checkbox-3053']").Click
    Application.Wait Now + TimeValue("00:00:05")
    Html.querySelector(".bsg-dropdown__footer button[type='button']").Click
End Sub

【问题讨论】:

  • 为什么点击第一个字段的Close button 和第三个.querySelector?我猜你至少需要指定第二个找到的元素:Html.querySelectorAll(".bsg-dropdown__footer button[type='button']")(1).Click。提交的整个网络表单应该是什么样子,每个字段?
  • 这需要登录吗?圆圈一直在为我呼呼。
  • @QHarr 它无需登录即可使用。目前有 1413 个担保债券类型的结果。
  • 不,不需要登录@QHarr。
  • :-( 所以该网站只是讨厌我。啊....它讨厌我的代理。已排序。

标签: html vba web-scraping internet-explorer-11


【解决方案1】:

您熟悉定时循环,因此您可以在下面添加超时。我通过 out 使用 css 选择器来定位元素。我监视呼呼圆圈的消失,以衡量各个点的页面负载。最后,我监视结果项是否存在 nodeList 节点。

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub GetCodes()
    Dim ie As New InternetExplorer, btn As Object, items As Object, i As Long
    With ie
        .Visible = True
        .Navigate2 "https://www.boerse-stuttgart.de/en/tools/product-search/bonds"

        While .Busy Or .readyState < 4: DoEvents: Wend

        Do
        Loop Until .document.querySelectorAll(".bsg-loader-ring__item").Length = 0


        .document.querySelector("#bsg-filters-btn-bgs-filter-3").Click
        .document.querySelector("#bsg-checkbox-3053").Click

        Set btn = .document.querySelector("#bsg-filters-menu-bgs-filter-3 .bsg-btn__label")

        Do
        Loop While btn.innerText = "Close"

        btn.Click

        Do
        Loop Until .document.querySelectorAll(".bsg-loader-ring__item").Length = 0
        Dim count As Long
        Do
            On Error Resume Next
            Set items = .document.querySelectorAll(".bsg-table__tr td:first-child")
            count = items.Length
            On Error GoTo 0
        Loop While count = 0

        For i = 0 To items.Length - 1
            Debug.Print items.item(i).innerText
        Next
        .Quit
    End With
End Sub

【讨论】:

  • 我没来检查@QHarr。让你知道它的表现。提前致谢。
  • 有任何问题请告诉我
  • 非常感谢。
猜你喜欢
  • 1970-01-01
  • 2017-08-26
  • 2016-04-03
  • 2020-09-10
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
相关资源
最近更新 更多