无法让我的脚本 运行 结束
Unable to let my script run through the end
我使用 ServerXMLHTTP
请求在 vba 中编写了一个脚本,以便能够使用 proxy
以及在其中设置 timeout
参数。当我 运行 脚本时,它似乎可以正常工作,但问题是 - 使用第一个代理后它卡住了。我希望这是 运行ning,直到没有任何代理可供使用。我定义这一行 While .readyState < 4: DoEvents: Wend
只是为了不让脚本冻结。 无论代理是否工作脚本都应该继续,对吧?
这是我试过的:
Sub MakeProxiedRequests()
Dim Http As New ServerXMLHTTP60, Html As New HTMLDocument
Dim elem As Object, proxyList As Variant, oProxy As Variant
proxyList = Array( _
"191.96.42.184:3129", _
"138.197.108.5:3128", _
"35.245.145.147:8080", _
"173.46.67.172:58517", _
"191.96.42.82:3129", _
"157.55.201.224:8080", _
"67.205.172.239:3128", _
"191.96.42.106:3129" _
)
For Each oProxy In proxyList
Debug.Print "trying with: " & oProxy
With Http
.Open "GET", "https://whosebug.com/questions/tagged/web-scraping", True
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setProxy 2, oProxy
.setTimeouts 600000, 600000, 15000, 15000 'I don't know the ideal timeout parameters
On Error Resume Next
.send
While .readyState < 4: DoEvents: Wend 'to let not freeze the script
Html.body.innerHTML = .responseText
Set elem = Html.querySelectorAll(".summary .question-hyperlink")
On Error GoTo 0
End With
If elem.Length > 0 Then
Debug.Print elem(0).innerText
Else:
Debug.Print "failed with: " & oProxy
End If
Next oProxy
End Sub
- 注意:脚本将始终产生相同的结果。但是,我的意图是保留脚本 运行ning 直到所有代理都被使用。
如何让我的脚本 运行 直到所有代理都用完?
可能的方法是控制请求总运行时间并限制它。还会检查任何 运行 时间错误。
Sub MakeProxiedRequests()
Const Timeout = "0:00:15"
Dim oHttp As New ServerXMLHTTP60
Dim oHtml As New HTMLDocument
Dim oElem As Object
Dim aProxyList
Dim sProxy
Dim t As Date
Dim bFailed As Boolean
aProxyList = Array( _
"191.96.42.184:3129", _
"138.197.108.5:3128", _
"35.245.145.147:8080", _
"173.46.67.172:58517", _
"191.96.42.82:3129", _
"157.55.201.224:8080", _
"67.205.172.239:3128", _
"191.96.42.106:3129" _
)
For Each sProxy In aProxyList
Debug.Print "Trying with: " & sProxy
With oHttp
.Open "GET", "https://whosebug.com/questions/tagged/web-scraping", True
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setProxy 2, sProxy
.setTimeouts 60000, 60000, 60000, 60000
.send
t = Now() + TimeValue(Timeout)
bFailed = False
On Error Resume Next
Do
If .readyState = 4 Then Exit Do
bFailed = (Now() > t) Or (Err.Number <> 0)
If bFailed Then Exit Do
DoEvents
Loop
On Error GoTo 0
If Not bFailed Then
oHtml.body.innerHTML = .responseText
Set oElem = oHtml.querySelectorAll(".summary .question-hyperlink")
bFailed = oElem.Length = 0
End If
End With
If Not bFailed Then
Debug.Print oElem(0).innerText
Else
Debug.Print "Failed with: " & sProxy
End If
Next
End Sub
我使用 ServerXMLHTTP
请求在 vba 中编写了一个脚本,以便能够使用 proxy
以及在其中设置 timeout
参数。当我 运行 脚本时,它似乎可以正常工作,但问题是 - 使用第一个代理后它卡住了。我希望这是 运行ning,直到没有任何代理可供使用。我定义这一行 While .readyState < 4: DoEvents: Wend
只是为了不让脚本冻结。 无论代理是否工作脚本都应该继续,对吧?
这是我试过的:
Sub MakeProxiedRequests()
Dim Http As New ServerXMLHTTP60, Html As New HTMLDocument
Dim elem As Object, proxyList As Variant, oProxy As Variant
proxyList = Array( _
"191.96.42.184:3129", _
"138.197.108.5:3128", _
"35.245.145.147:8080", _
"173.46.67.172:58517", _
"191.96.42.82:3129", _
"157.55.201.224:8080", _
"67.205.172.239:3128", _
"191.96.42.106:3129" _
)
For Each oProxy In proxyList
Debug.Print "trying with: " & oProxy
With Http
.Open "GET", "https://whosebug.com/questions/tagged/web-scraping", True
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setProxy 2, oProxy
.setTimeouts 600000, 600000, 15000, 15000 'I don't know the ideal timeout parameters
On Error Resume Next
.send
While .readyState < 4: DoEvents: Wend 'to let not freeze the script
Html.body.innerHTML = .responseText
Set elem = Html.querySelectorAll(".summary .question-hyperlink")
On Error GoTo 0
End With
If elem.Length > 0 Then
Debug.Print elem(0).innerText
Else:
Debug.Print "failed with: " & oProxy
End If
Next oProxy
End Sub
- 注意:脚本将始终产生相同的结果。但是,我的意图是保留脚本 运行ning 直到所有代理都被使用。
如何让我的脚本 运行 直到所有代理都用完?
可能的方法是控制请求总运行时间并限制它。还会检查任何 运行 时间错误。
Sub MakeProxiedRequests()
Const Timeout = "0:00:15"
Dim oHttp As New ServerXMLHTTP60
Dim oHtml As New HTMLDocument
Dim oElem As Object
Dim aProxyList
Dim sProxy
Dim t As Date
Dim bFailed As Boolean
aProxyList = Array( _
"191.96.42.184:3129", _
"138.197.108.5:3128", _
"35.245.145.147:8080", _
"173.46.67.172:58517", _
"191.96.42.82:3129", _
"157.55.201.224:8080", _
"67.205.172.239:3128", _
"191.96.42.106:3129" _
)
For Each sProxy In aProxyList
Debug.Print "Trying with: " & sProxy
With oHttp
.Open "GET", "https://whosebug.com/questions/tagged/web-scraping", True
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setProxy 2, sProxy
.setTimeouts 60000, 60000, 60000, 60000
.send
t = Now() + TimeValue(Timeout)
bFailed = False
On Error Resume Next
Do
If .readyState = 4 Then Exit Do
bFailed = (Now() > t) Or (Err.Number <> 0)
If bFailed Then Exit Do
DoEvents
Loop
On Error GoTo 0
If Not bFailed Then
oHtml.body.innerHTML = .responseText
Set oElem = oHtml.querySelectorAll(".summary .question-hyperlink")
bFailed = oElem.Length = 0
End If
End With
If Not bFailed Then
Debug.Print oElem(0).innerText
Else
Debug.Print "Failed with: " & sProxy
End If
Next
End Sub