2012-04-12 10 views
1

Ich habe ein Programm, das Inhalt von URLs in einer Datenbank gespeichert. Ich verwende beautifulsoup, urllib2 zum Ergreifen des Inhalts. Wenn ich das Ergebnis ausspreche, sehe ich, dass das Programm abstürzt, wenn es einen Fehler 403 (wie es aussieht) ein 403 Fehler. Wie verhindere ich, dass mein Programm bei 403/404 Fehlern abstürzt?Python, urllib2, stürzt auf 404 Fehler

Relevante Ausgabe:

Traceback (most recent call last): 
    File "web_content.py", line 29, in <module> 
    grab_text(row) 
    File "web_content.py", line 21, in grab_text 
    f = urllib2.urlopen(row) 
    File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen 
    return _opener.open(url, data, timeout) 
    File "/usr/lib/python2.7/urllib2.py", line 400, in open 
    response = meth(req, response) 
    File "/usr/lib/python2.7/urllib2.py", line 513, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "/usr/lib/python2.7/urllib2.py", line 438, in error 
    return self._call_chain(*args) 
    File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain 
    result = func(*args) 
    File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 403: Forbidden 
+1

Sie können Ausnahmen verwenden – Asterisk

+0

@Asterisk ich sehe. Neu bei Python. Vielen Dank! – yayu

Antwort