私はPython初心者の方です。これは私の最初の投稿です。 :) 投稿する前に私はGoogleとstackoverflowを検索したが、私の問題に似た何かを見つけることができないようだ。Python 3.5.2:socket.timeout例外のために型エラーが発生する
私はウェブサイトをポーリングしてコンテンツを取得するスクリプトを持っています。 それは何時間も問題なく動作しますが、ソケットタイムアウトが発生した場合、例外が発生してもスクリプトは型エラーをスローします。
私は何か明白なものを見逃していると確信していますが、私の指をそれに置くことはできません。
コード:
timingout = 10
def get_url(url):
try:
sock = urllib.request.urlopen(url, timeout=timingout)
orig_html = sock.read()
html = orig_html.decode("utf-8", errors="ignore").encode('cp1252', errors='ignore')
sock.close()
except KeyboardInterrupt:
# Kill program if Control-C is pressed
sys.exit(0)
except urllib.error.URLError as e:
print("***Error= Page ", e.reason)
return
except timingout:
print("socket timed out - URL: %s", url)
else:
# See if site is Down or errors eg: 404
if html == None:
print ("page contains no content!?!")
return ''
# See if site is complaining
elif html == site_overload:
if _verbose:
print('ERROR: Too many requests - SLEEPING 600 secs')
time.sleep(600)
return ''
# If not, we are good
elif html:
return html
エラー:任意の提案&助けを事前に
return self._sock.recv_into(b)
**socket.timeout: timed out**
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 201, in <module>
main()
File "test.py", line 140, in main
http_text = get_text(site_id)
File "test.py", line 110, in get_text
return get_url(url)
File "test.py", line 59, in get_url
except timingout:
**TypeError: catching classes that do not inherit from BaseException is not allowed**
ありがとう!
お返事ありがとうございます。私は今何が行われたかを見ます。また、try文を分割することについてのアドバイスに従います。私はこれで非常に新しいので、本当に参考に感謝:) – GpT