【问题标题】:VBA wait for everything in a webpage to completely loadVBA 等待网页中的所有内容完全加载
【发布时间】:2019-03-08 13:12:32
【问题描述】:

我试图在导航到某个网站后从网站中提取一些信息,但我似乎无法等到它完全加载。我一直在尝试循环,直到 (0) 处的类包含文本。有谁知道我做错了什么?

Sub test()

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

Dim elements2 As IHTMLElementCollection

    IE.Navigate "https://www.facebook.com/marketplace/item/955559644646354/"
        Do While IE.Busy Or IE.readyState <> 4
            DoEvents
        Loop
Dim x
x = 0
Do Until x = 1
Set elements2 = IE.document.getElementsByClassName("_3cgd")

If WorksheetFunction.IsText(elements2(0).innerText) = True Then
    MsgBox ((elements2(0).innerText))
    x = 1
  Else
  Application.Wait Now + #12:00:01 AM#
  End If
Loop

End Sub

【问题讨论】:

  • 我实际上并没有在页面上看到该课程。我猜这可能是一个地区的事情。您还可以指出您希望页面之外的实际信息吗? (我看到一个带有保修的全新霓虹蓝和红色 Nintendo Switch 的广告)。你已经有了很好的答案,但我仍然有兴趣看到任何其他方法来解决这个问题。
  • 是的,我无法再加载该特定链接,因此该项目可能已被除名。我希望得到的信息是卖家的名字。

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


【解决方案1】:

试试这样的东西(未经测试)

Dim t, elements2, txt

t = Timer
txt = ""

Do 
    Set elements2 = IE.document.getElementsByClassName("_3cgd")
    If elements2.length > 0 Then
        txt = elements2(0).innerText
        If Len(txt) > 0 Then Exit Do
    End If

    If (Timer - t) > 10 Then Exit Do 'exit if too long waiting

    Application.Wait Now + TimeSerial(0, 0, 1)
Loop

【讨论】:

    【解决方案2】:

    您可以尝试在下面添加几行等待网页完全加载。

    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
        Do While IE.ReadyState = 4: DoEvents: Loop   'Do While
        Do Until IE.ReadyState = 4: DoEvents: Loop   'Do Until
    

    以下是完整的工作示例:

    Sub Automate_IE_Load_Page()
    'This will load a webpage in IE
        Dim i As Long
        Dim URL As String
        Dim IE As Object
        Dim objElement As Object
        Dim objCollection As Object
    
        'Create InternetExplorer Object
        Set IE = CreateObject("InternetExplorer.Application")
    
        'Set IE.Visible = True to make IE visible, or False for IE to run in the background
        IE.Visible = True
    
        'Define URL
        URL = "https://www.automateexcel.com/excel/"
    
        'Navigate to URL
        IE.Navigate URL
    
        ' Statusbar let's user know website is loading
        Application.StatusBar = URL & " is loading. Please wait..."
    
        ' Wait while IE loading...
        'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
        Do While IE.ReadyState = 4: DoEvents: Loop   'Do While
        Do Until IE.ReadyState = 4: DoEvents: Loop   'Do Until
    
        'Webpage Loaded
        Application.StatusBar = URL & " Loaded"
    
        'Unload IE
        Set IE = Nothing
        Set objElement = Nothing
        Set objCollection = Nothing
    
    End Sub
    

    此外,您可以尝试根据您的要求修改此代码示例。

    参考:

    (1)Automate Internet Explorer (IE) Using VBA

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 2021-02-05
      • 2017-02-06
      • 2013-11-24
      • 2019-02-10
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2021-09-22
      相关资源
      最近更新 更多