BeautifulSoup中的捕捉异常(PY)

时间:2014-11-30 11:10:13

标签: python

我制作了一个解析和html文件并打印一些数据的程序。 该程序使用Beautiful soup解析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))

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以显示BeautifulSoup为您提供的例外情况:

Python v3:

  

尝试:

 ... 
     

除了异常为错误:

 print(error)

Python v2:

  

除了异常,错误:

      print (error)