Python 如何显示错误消息连接?

Python 如何显示错误消息连接?,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,请告诉我是否捕捉到可能的错误 import requests try: req = requests.get('http://google.com') except FileNotFoundError as exc: print('Error open url. ', exc) 或者FileNotFoundError仅适用于文件系统中的文件?在这种情况下,我仍然使用上述代码的异常类型是什么?是由请求库引发的异常列表FileNotFoundError不在其中 你应该这样做: i

请告诉我是否捕捉到可能的错误

import requests
try:
    req = requests.get('http://google.com')
except FileNotFoundError  as exc:
    print('Error open url. ', exc)
或者FileNotFoundError仅适用于文件系统中的文件?在这种情况下,我仍然使用上述代码的异常类型是什么?

是由
请求
库引发的异常列表
FileNotFoundError
不在其中

你应该这样做:

import requests

try:
    req = requests.get('http://google.com')
except requests.exceptions.HTTPError as exc:
    print('HTTP error', exc)
您可以根据需要捕获其他异常:

Python 3.3.2+ (default, Oct  9 2013, 14:50:09) 
>>> import requests
>>> requests.get('googleeeeeeee2323235.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    ...
    "Perhaps you meant http://{0}?".format(url))
requests.exceptions.MissingSchema: Invalid URL 'googleeeeeeee2323235.com': No schema supplied. Perhaps you meant http://googleeeeeeee2323235.com?
>>> requests.get('http://googleeeeeeee2323235.com')
Traceback (most recent call last):
  ...
  File "/usr/lib/python3.3/socket.py", line 417, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
    ...
    raise MaxRetryError(self, url, e)
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='googleeeeeeee2323235.com', port=80): Max retries exceeded with url: / (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
    ...
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='googleeeeeeee2323235.com', port=80): Max retries exceeded with url: / (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)
>>> 
Python 3.3.2+(默认,2013年10月9日,14:50:09)
>>>导入请求
>>>requests.get('googleeee2323235.com')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
...
“您的意思可能是http://{0}?”。格式(url))
requests.exceptions.MissingSchema:无效URL“Googleeee2323235.com”:未提供架构。也许你的意思是http://googleeeeeeee2323235.com?
>>>请求。获取('http://googleeeeeeee2323235.com')
回溯(最近一次呼叫最后一次):
...
文件“/usr/lib/python3.3/socket.py”,第417行,在create_connection中
对于getaddrinfo(主机、端口、0、SOCK_流)中的res:
socket.gaierror:[Errno-2]名称或服务未知
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
...
raise MaxRetryError(self、url、e)
requests.packages.urllib3.exceptions.MaxRetryError:HTTPConnectionPool(host='Googleeee2323235.com',port=80):url:/(导致的原因:[Errno-2]名称或服务未知)
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
...
升起连接器错误(e)
requests.exceptions.ConnectionError:HTTPConnectionPool(host='Googleeee2323235.com',port=80):url:/(导致的原因是:[Errno-2]名称或服务未知)
>>> 
因此,对于未知主机,您应该捕获
请求.exceptions.ConnectionError


或者,您可能希望捕获所有
请求的基本异常。请查阅文档或源代码以了解它是什么。

这可能会有所帮助-@Ashish提供的链接对我开放,但您的链接没有,我尝试了3-4次。at req=requests.get(')您的脚本不会输出“HTTP error”,即使我在打开链接时遇到问题,但它之前已经打开得很好。也许是暂时的问题。试试缓存版本-@Sergey,我已经更新了我的帖子。您应该首先阅读更多关于Python异常以及如何处理它们的内容: