requests
を使っていると,get
すると応答がなく,フリーズしたみたいにスクリプトが止まってしまうことがあり,
その度に手動でスクリプトを強制的に終わらせてました...
毎回毎回手動で終了させるのはメンドすぎる.どうすればいいんだってことでググったら以下のサイトが参考になりました.
というわけで,こんな感じでTimeout設定をする.それでもダメなら強制的に終了させるしかないか...
import requests
from time import time
while True:
try:
d=requests.get(url, timeout=(3.0, 7.5))
break
except requests.exceptions.ConnectionError:
print('ConnectionError')
sleep(5)
continue
except requests.exceptions.Timeout:
print('Timeout')
sleep(5)
continue
mechanize
ならこんな感じか?タイムアウトエラーの補足方法が分からん...
from mechanize import Browser
from time import sleep
import urllib
while True:
try:
br.open(url, timeout=10)
break
except urllib.error.URLError:
print('URLError')
sleep(5)
continue
except urllib.error.HTTPError as e:
print('Timeout')
sleep(5)
continue