本文介绍了VBA XML V6.0如何使其等待页面加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在努力寻找答案,我似乎找不到任何有用的东西.
I have been pulling my hair out trying to find an answer for this and I cant seem to find anything useful.
基本上,我是从网站上撤出的,而您在该页面上时会加载更多项目.我希望我的代码在完成加载后提取最终数据,但是不确定如何使XML httprequest等待.
Basically I am pulling from a website that loads more items on it while you are on the page. I would like my code to pull the final data after its done loading but am not sure how to make XML httprequest wait for that.
Sub pullsomesite()
Dim httpRequest As XMLHTTP
Dim DataObj As New MSForms.DataObject
Set httpRequest = New XMLHTTP
Dim URL As String
URL = "somesite"
With httpRequest
.Open "GET", URL, True
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Application.Wait Now + TimeValue("0:02:00")
.send
' ... after the .send call finishes, you can check the server's response:
End With
While Not httpRequest.readyState = 4 '<---------- wait
Wend
If httpRequest.Status = 200 Then
Application.Wait Now + TimeValue("0:00:30")
Debug.Print httpRequest.responseText
'continue...
End If
'Debug.Print httpRequest.Status
'Debug.Print httpRequest.readyState
'Debug.Print httpRequest.statusText
DataObj.SetText httpRequest.responseText
DataObj.PutInClipboard
With Sheets("Sheet1")
.Activate
.Range("A1000000").End(xlUp).Offset(1, 0).Select
.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
End With
End Sub
屏幕截图
推荐答案
尝试等待响应的就绪状态和主体不包含单词"Updating":
Try waiting for the ready state and body of the response not to contain the word "Updating":
Option Explicit
Sub pullSomeSite()
Dim httpRequest As XMLHTTP
Set httpRequest = New XMLHTTP
Dim URL As String
URL = "SomeSite"
With httpRequest
.Open "GET", URL, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send
End With
With httpRequest
While Not .ReadyState = 4 '<---------- wait
Application.Wait Now + TimeValue("0:00:01")
Wend
If .Status = 200 Then
While InStr(1, .responseText, "Updating", 0) > 0 '<---------- wait again
Application.Wait Now + TimeValue("0:00:01")
Wend
Debug.Print .responseText
'continue...
End If
End With
End Sub
这篇关于VBA XML V6.0如何使其等待页面加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!