Html VBA等待网页中的所有内容完全加载
我试图在导航到某个网站后从中提取一些信息,但我似乎迫不及待地等待它完全加载。我一直在尝试循环,直到(0)处的类包含文本。有人知道我做错了什么吗Html VBA等待网页中的所有内容完全加载,html,vba,internet-explorer,web-scraping,extract,Html,Vba,Internet Explorer,Web Scraping,Extract,我试图在导航到某个网站后从中提取一些信息,但我似乎迫不及待地等待它完全加载。我一直在尝试循环,直到(0)处的类包含文本。有人知道我做错了什么吗 Sub test() Dim IE As Object Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True Dim elements2 As IHTMLElementCollection IE.Navigate "https://www.facebo
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
子测试()
模糊的物体
设置IE=CreateObject(“InternetExplorer.Application”)
可见=真实
作为IHTMLElementCollection的调光元件2
即“导航”https://www.facebook.com/marketplace/item/955559644646354/"
在忙或准备状态4时执行
多芬特
环
暗x
x=0
直到x=1为止
Set elements2=IE.document.getElementsByClassName(“\u 3cgd”)
如果工作表function.IsText(elements2(0).innerText)=True,则
MsgBox((elements2(0.innerText))
x=1
其他的
申请。现在等待+#12:00:01 AM#
如果结束
环
端接头
尝试类似的方法(未经测试)
您可以尝试在下面添加行,以等待完全加载网页
' 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) 我在页面上没有看到这个类。我猜这可能是一个地区的事情。您是否也可以指出您希望从页面中获得的实际信息?(我看到一则关于全新霓虹蓝&红任天堂交换机的广告,带有保修)。你已经有了很好的答案,但我仍然有兴趣看到任何其他的方法来攻击这个。是的,我不能再加载那个特定的链接了,所以这个项目可能已经被摘牌了。我想知道的是卖家的名字。
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