Python response=requests.get无法访问URL时出错

Python response=requests.get无法访问URL时出错,python,exception,python-requests,Python,Exception,Python Requests,我正在尝试编写一个脚本,其中一部分必须检查URL是否可用。问题是,当我收到回复200404等时,程序运行良好,我可以处理回复,但当无法访问URL时,程序会出现以下错误。 这是代码的一部分: response = requests.get(url) print (response) 错误: Traceback (most recent call last): File "C:\Python\lib\site-packages\requests\packages\urllib3\connect

我正在尝试编写一个脚本,其中一部分必须检查URL是否可用。问题是,当我收到回复200404等时,程序运行良好,我可以处理回复,但当无法访问URL时,程序会出现以下错误。 这是代码的一部分:

response = requests.get(url)
print (response)
错误:

Traceback (most recent call last):
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "C:\Python\lib\site-packages\requests\packages\urllib3\util\connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "C:\Python\lib\socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 345, in _make_request
    self._validate_conn(conn)
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 844, in _validate_conn
    conn.connect()
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 284, in connect
    conn = self._new_conn()
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 150, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x03EC9970>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
回溯(最近一次呼叫最后一次):
文件“C:\Python\lib\site packages\requests\packages\urllib3\connection.py”,第141行,位于康涅狄格州的新
(self.host、self.port)、self.timeout、**额外功率)
文件“C:\Python\lib\site packages\requests\packages\urllib3\util\connection.py”,第60行,位于create\u connection中
对于socket.getaddrinfo(主机、端口、系列、socket.SOCK\u流)中的res:
文件“C:\Python\lib\socket.py”,第743行,位于getaddrinfo中
对于_socket.getaddrinfo(主机、端口、系列、类型、协议、标志)中的res:
socket.gaierror:[Errno 11001]getaddrinfo失败
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\Python\lib\site packages\requests\packages\urllib3\connectionpool.py”,第600行,在urlopen中
分块=分块)
文件“C:\Python\lib\site packages\requests\packages\urllib3\connectionpool.py”,第345行,在请求中
自我验证连接(连接)
文件“C:\Python\lib\site packages\requests\packages\urllib3\connectionpool.py”,第844行,位于\u validate\u conn
连接
文件“C:\Python\lib\site packages\requests\packages\urllib3\connection.py”,第284行,在connect中
conn=自我。_new_conn()
文件“C:\Python\lib\site packages\requests\packages\urllib3\connection.py”,第150行,位于康涅狄格州的新
self,“无法建立新连接:%s”%e)
requests.packages.urllib3.exceptions.NewConnectionError::未能建立新连接:[Errno 11001]getaddrinfo失败
有解决办法吗? 如果我能将脚本设置为打印一行,比如URL不可用且存在,那就太好了。

请求
文档的内容表明您应该能够在此处捕获以下内容:

使用不存在的主机名进行快速演示:

>>> import requests
>>> try:
...     response = requests.get("http://no_such_site_exists.imsure")
... except requests.ConnectionError:
...     print("Can't connect to the site, sorry")
... else:
...     print(response)
...
Can't connect to the site, sorry

那么就这样?看。您是否尝试过此方法?您是否在定位要捕获的异常时遇到问题?您是否考虑过捕获和处理该异常?
>>> import requests
>>> try:
...     response = requests.get("http://no_such_site_exists.imsure")
... except requests.ConnectionError:
...     print("Can't connect to the site, sorry")
... else:
...     print(response)
...
Can't connect to the site, sorry