我需要訪問一個url,如果它給了我一個HTTPError,我需要等5分鐘,然後再試一次(這適用於此特定網站)。它看起來像代碼不承認except子句,它仍然立即給我一個HTTPError(無需等待5分鐘)。即使使用「try and except」子句,仍然會收到HTTP404錯誤
import urllib2, datetime, re,os, requests
from time import sleep
import time
from dateutil.relativedelta import relativedelta
from requests.exceptions import HTTPError, ConnectionError
from bs4 import BeautifulSoup
try:
resp = requests.get(url)
except HTTPError:
while True:
print "Wait."
time.sleep(305)
resp = requests.get(url)
except ConnectionError:
while True:
print "Wait."
time.sleep(305)
resp = requests.get(url)
爲什麼'if err.code == 404'聲明? – GLHF