我被困在Python请求中的异常处理中

时间:2017-07-22 05:41:08

标签: python beautifulsoup python-requests

提供者。

我正在使用python请求模块编写一个获取Web程序的标题。 我想做错误处理,但我被卡住了。

这是我的代码。

for i in result:
try: 
    print("URL : {}".format(i[0]))
    req = requests.get(i[0], headers=headers, timeout=15)
    html = req.text
    soup = BeautifulSoup(html, 'html.parser')
    title = soup.title.string
    print(title)

except requests.ConnectionError as e:
    print("Error : Network Connection Error <{}>".format(e))

except requests.Response.raise_for_status() as e:
    print("Error: Invalid Response Error.")

except requests.exceptions.Timeout as e:
    print("Error: Timeout Error.")

except requests.TooManyRedirects as e:
    print("Error: Too many Redirects")

except:
    print("Error: Unknown Error.")

这是Python控制台的答案:

URL : url.com
Nice URL
URL : http://0x92B9BDB9
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.4/http/client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.4/http/client.py", line 1126, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.4/http/client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.4/http/client.py", line 922, in _send_output
    self.send(msg)
  File "/usr/lib/python3.4/http/client.py", line 857, in send
    self.connect()
  File "/usr/lib/python3.4/http/client.py", line 834, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python3.4/socket.py", line 512, in create_connection
    raise err
  File "/usr/lib/python3.4/socket.py", line 503, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 330, in send
    timeout=timeout
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 562, in urlopen
    body=body, headers=headers)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 391, in _make_request
    (self.host, timeout_obj.connect_timeout))
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connectionpool.HTTPConnectionPool object at 0x7f0e820c69b0>, 'Connection to 0x92B9BDB9 timed out. (connect timeout=15)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "gettitle.py", line 49, in <module>
    req = requests.get(i[0], headers=headers, timeout=15)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 455, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 558, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 387, in send
    raise Timeout(e)
requests.exceptions.Timeout: (<urllib3.connectionpool.HTTPConnectionPool object at 0x7f0e820c69b0>, 'Connection to 0x92B9BDB9 timed out. (connect timeout=15)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "gettitle.py", line 58, in <module>
    except requests.Response.raise_for_status() as e:
TypeError: raise_for_status() missing 1 required positional argument: 'self'

我该如何解决? 非常感谢让我知道!

1 个答案:

答案 0 :(得分:0)

我在使用 request.get 时遇到了同样的问题,我一直遇到和您一样的问题。我发现我正在使用我的 VPN。当我关闭它时,一切都恢复了正常!