【问题标题】:Trouble making my parser scroll downward无法让我的解析器向下滚动
【发布时间】:2018-09-16 19:43:53
【问题描述】:

我在 vba 中编写了一个脚本来解析网页中的一些信息。问题是在从该网页上抓取任何信息之前,我需要让我的抓取器向下滚动几次。这是我卡住的地方。我需要滚动的部分是左侧窗口。任何帮助将不胜感激。

Website Link

这是我的尝试:

Sub Make_Scroll()
    Dim HTML As HTMLDocument, post As Object
    Dim Scroll As Object, URL$

    URL = "replace_with_above_link"

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate URL
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set HTML = .document

        Application.Wait Now + TimeValue("00:00:003")

        Set Scroll = HTML.querySelector(".iScrollLoneScrollbar[style*='z-index: 9999;']")
        Scroll.scrollTop = Scroll.scrollHeight
    End With
End Sub

制作滚动的内容可能在以下部分:

<div class="iScrollVerticalScrollbar iScrollLoneScrollbar" style="position: absolute; z-index: 9999; width: 7px; bottom: 2px; top: 2px; right: 1px; overflow: hidden;"><div class="iScrollIndicator" style="box-sizing: border-box; position: absolute; background: rgba(0, 0, 0, 0.5); border: 1px solid rgba(255, 255, 255, 0.9); border-radius: 3px; width: 100%; transition-duration: 0ms; display: block; height: 8px; transform: translate(0px, 26px) translateZ(0px); transition-timing-function: cubic-bezier(0.1, 0.57, 0.1, 1);"></div></div>

【问题讨论】:

  • 该页面没有标准 (HTML) 滚动条。
  • 不管它是什么,应该有任何方法可以使用 vba 来解决问题。
  • 试试 XHR 而不是 IE。
  • 我对他们的数据完全不感兴趣。我只是想知道如何使用 IE 来处理这种情况。

标签: vba excel scroll web-scraping internet-explorer-11


【解决方案1】:

看看下面的例子:

Option Explicit

Sub Test()

    ' Add references
    ' Microsoft XML, v6.0
    ' Microsoft HTML Object Library

    Dim sResponse As String
    Dim aResult() As String
    Dim i As Long
    Dim oElement As HTMLDivElement

    With New XMLHTTP
        .Open "POST", "http://catalogo.marmomac.it/ajax/getListEspositori.php", False
        .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
        .Send "start=0&count=5000"
        sResponse = .ResponseText
    End With
    With New HTMLDocument
        .body.innerHTML = sResponse
        i = 1
        For Each oElement In .getElementsByClassName("elemento")
            ThisWorkbook.Sheets(1).Cells(i, 1) = oElement.innerText
            i = i + 1
        Next
    End With

End Sub

我的输出如下,总共有 1669 项:

【讨论】:

  • 除了使用 IE 滚动该站点之外,我并没有期待任何解决方案,但您已经用您最好的方法迷住了我。这真是太好了,我的意思是,你写剧本的方式。谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 2014-07-10
  • 2021-02-11
  • 2014-03-15
  • 2016-05-19
相关资源
最近更新 更多