簡體   English   中英

無法讓我的腳本運行到最后

[英]Unable to let my script run through the end

我使用ServerXMLHTTP請求在vba中編寫了一個腳本,以便能夠使用proxy以及在其中設置timeout參數。 當我運行腳本時,它似乎正在工作,但問題是 - 它在使用第一個代理后卡住了。 我希望這個可以運行,直到沒有代理使用。 我定義了這一行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://stackoverflow.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
  • 注意:腳本將始終產生相同的結果。 但是,我的目的是讓腳本保持運行,直到所有代理都被使用。

如何讓我的腳本運行直到所有代理都用完為止?

可能的方法是控制請求總體經過的時間並限制它。 還會檢查任何運行時錯誤。

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://stackoverflow.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM