[英]Catching Exception in BeautifulSoup (PY)
我已经制作了一个程序,可以解析和html文件并打印一些数据。 该程序使用Beautiful汤解析html。 该程序在直到没有连接之前都可以正常工作。 当发生这种情况时,会出现一系列错误(我知道是一团糟):
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 516, in urlopen
body=body, headers=headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 308, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1086, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 924, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 859, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 146, in connect
conn = self._new_conn()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 125, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 64, in create_connection
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/adapters.py", line 362, in send
timeout=timeout
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 559, in urlopen
_pool=self, _stacktrace=stacktrace)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/util/retry.py", line 245, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/packages/six.py", line 309, in reraise
raise value.with_traceback(tb)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 516, in urlopen
body=body, headers=headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 308, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1086, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 924, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 859, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 146, in connect
conn = self._new_conn()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 125, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 64, in create_connection
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', gaierror(8, 'nodename nor servname provided, or not known'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/n1/Desktop/PP3-Pfahlf/main.py", line 235, in <module>
from davos import *
File "/Users/n1/Desktop/PP3-Pfahlf/davos.py", line 13, in <module>
response = requests.get('http://www.davos.ch/en/experience/winter/ski-snowboard/piste-report.html')
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/api.py", line 60, in get
return request('get', url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 569, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(8, 'nodename nor servname provided, or not known'))
if i do
try:
...
except Execption:
print('No connection')
它有效,但不是连接错误的规范。
while if i do:
try:
...
except ConnectionError:
print('No connection')
其他错误将会出现。
有人可以帮我吗?
编辑:
部分代码如下所示:
response = requests.get('http://www.davos.ch/en/experience/winter/ski-snowboard/piste-report.html')
data = response.text
soup = BeautifulSoup(data)def set_items_status():
select_status = soup.select(".opening")
regEx = re.compile('.*?<div.*?>(\w)</div>.*?', re.DOTALL)
for element in select_status:
status_items_lifts.append(regEx.search(str(element)).group(1))
from BeautifulSoup import BeautifulSoup
from requests import ConnectionError
import requests
def set_items_status():
status_items_lifts = []
try:
response = requests.get('http://www.davos.ch/en/experience/winter/ski-snowboard/piste-report.html')
data = response.text
soup = BeautifulSoup(data)
select_status = soup.select(".opening")
regEx = re.compile('.*?<div.*?>(\w)</div>.*?', re.DOTALL)
for element in select_status:
status_items_lifts.append(regEx.search(str(element)).group(1))
except(ConnectionError, Exception), e:
print "Exception is :", e
set_items_status()
您必须在元组中传递异常,以便它可以从左到右查看。
在您的情况下,它将首先查找ConnectionError
异常,如果连接正常,它将查找其他exceptions
。
class ConnectionError(RequestException)
| A Connection error occurred.
|
| Method resolution order:
| ConnectionError
| RequestException
| exceptions.IOError
| exceptions.EnvironmentError
| exceptions.StandardError
| exceptions.Exception
| exceptions.BaseException
class Exception(BaseException) | Common base class for all non-exit exceptions. | | Method resolution order: | Exception | BaseException
您可以使用以下方法显示BeautifulSoup给您的异常:
Python v3:
尝试:
...
除了Exception作为错误:
print(error)
Python v2:
除了异常,错误:
print (error)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.