我是一个Python新手,这是我第一篇关于stackoverflow的帖子,所以请耐心等待。 :) 在发布之前我已经搜索过google和stackoverflow,但似乎无法找到与我的问题相似的任何内容。
我有一个用于轮询网站并检索内容的脚本。 它工作好几个小时但是如果它遇到套接字超时,脚本会抛出一个类型错误,即使我有一个例外。
我确定我错过了一些明显的东西,但是我不能指责它。
代码:
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**
提前感谢任何建议&amp;帮助!
答案 0 :(得分:0)
这是因为尝试使用timingout
来捕获异常。 timingout
是一个整数对象,而except
语句只接受从BaseException
类继承的对象。
删除except
因为它没有做任何事情。还要考虑修改try
语句以仅包含单个操作。它将使故障排除更容易,并防止您在发生错误时不得不在以后分解try
语句。