[英]Catching timeout errors with ruby mechanize
我有一個機械化功能可以讓我退出網站,但在非常罕見的情況下它會讓我失望。 該功能涉及轉到特定頁面,然后單擊注銷按鈕。 偶爾機械化在進入注銷頁面或單擊注銷按鈕時會出現超時,代碼崩潰。 所以我進行了一次小規模的救援,它似乎正如第一段代碼所示。
def logmeout(agent)
page = agent.get('http://www.example.com/')
agent.click(page.link_with(:text => /Log Out/i))
end
Logmeout with rescue:
def logmeout(agent)
begin
page = agent.get('http://www.example.com/')
agent.click(page.link_with(:text => /Log Out/i))
rescue Timeout::Error
puts "Timeout!"
retry
end
end
假設我正確地理解了救援,即使只是點擊超時,它也會做兩個動作,所以為了提高效率,我想知道我是否可以在這種情況下使用proc並傳遞代碼塊。 會這樣的工作:
def trythreetimes
tries = 0
begin
yield
rescue
tries += 1
puts "Trying again!"
retry if tries <= 3
end
end
def logmeout(agent)
trythreetimes {page = agent.get('http://www.example.com/')}
trythreetimes {agent.click(page.link_with(:text => /Log Out/i))}
end
請注意,在我的trythreetimes函數中,我將其保留為通用救援,因此該功能將更具可重用性。
非常感謝任何人提供的任何幫助,我知道這里有幾個不同的問題,但它們都是我想要學習的東西!
我認為你最好將Mechanize::HTTP::Agent::read_timeout
屬性設置為合理的秒數,如2或5,而不是在一些機械化請求上重試一些超時,無論如何都要阻止此請求的超時錯誤。
然后,似乎您的注銷過程只需要訪問簡單的HTTP GET請求。 我的意思是沒有表格可以填寫,所以沒有HTTP POST請求。 所以,如果我是你,我會優先檢查頁面源代碼(使用Firefox或Chrome的Ctrl + U),以便識別agent.click(page.link_with(:text => /Log Out/i))
所達到的鏈接agent.click(page.link_with(:text => /Log Out/i))
它應該更快,因為這些類型的頁面通常是空白的,而Mechanize不必在內存中加載完整的html網頁。
這是我更喜歡使用的代碼:
def logmeout(agent)
begin
agent.read_timeout=2 #set the agent time out
page = agent.get('http://www.example.com/logout_url.php')
agent.history.pop() #delete this request in the history
rescue Timeout::Error
puts "Timeout!"
puts "read_timeout attribute is set to #{agent.read_timeout}s" if !agent.read_timeout.nil?
#retry #retry is no more needed
end
end
但你也可以使用你的重試功能:
def trythreetimes
tries = 0
begin
yield
rescue Exception => e
tries += 1
puts "Error: #{e.message}"
puts "Trying again!" if tries <= 3
retry if tries <= 3
puts "No more attempt!"
end
end
def logmeout(agent)
trythreetimes do
agent.read_timeout=2 #set the agent time out
page = agent.get('http://www.example.com/logout_url.php')
agent.history.pop() #delete this request in the history
end
end
希望能幫助到你 ! ;-)
使用mechanize 1.0.0我從不同的錯誤來源得到了這個問題。
在我的情況下,我被代理阻止,然后是SSL。 這對我有用:
ag = Mechanize.new
ag.set_proxy('yourproxy', yourport)
ag.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
ag.get( url )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.