Frage

Ich bin mit diesem einfachen Code

for l in bios:
    OpenThisLink = url + l
    response = urllib2.urlopen(OpenThisLink)

etwa 200 Urls zu öffnen und suchen, um sie mit regex (und BeautifulSoup), aber nach einem Dutzend oder so bekomme ich diese Fehler und IDLE quitt. Was meinen sie? Wie kann ich sie behandeln?

Danke.

Traceback (most recent call last):

  File "\PROJECTS\JD\jd10.py", line 15, in <module> response = urllib2.urlopen(OpenThisLink)

  File "C:\Python26\lib\urllib2.py", line 124, in urlopen return _opener.open(url, data, timeout)

  File "C:\Python26\lib\urllib2.py", line 389, in open response = meth(req, response)

  File "C:\Python26\lib\urllib2.py", line 502, in http_response 'http', request, response, code, msg, hdrs)

  File "C:\Python26\lib\urllib2.py", line 421, in error result = self._call_chain(*args)

  File "C:\Python26\lib\urllib2.py", line 361, in _call_chain result = func(*args)

  File "C:\Python26\lib\urllib2.py", line 597, in http_error_302 return self.parent.open(new)

  File "C:\Python26\lib\urllib2.py", line 389, in open response = meth(req, response)

  File "C:\Python26\lib\urllib2.py", line 502, in http_response 'http', request, response, code, msg, hdrs)

  File "C:\Python26\lib\urllib2.py", line 421, in error result = self._call_chain(*args)

  File "C:\Python26\lib\urllib2.py", line 361, in _call_chain result = func(*args)

  File "C:\Python26\lib\urllib2.py", line 597, in http_error_302 return self.parent.open(new)

  File "C:\Python26\lib\urllib2.py", line 389, in open response = meth(req, response)

  File "C:\Python26\lib\urllib2.py", line 502, in http_response 'http', request, response, code, msg, hdrs)

  File "C:\Python26\lib\urllib2.py", line 427, in error return self._call_chain(*args)

  File "C:\Python26\lib\urllib2.py", line 361, in _call_chain result = func(*args)

  File "C:\Python26\lib\urllib2.py", line 510, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 404: Not Found
  
    
      

    
  
War es hilfreich?

Lösung

Der Fehler ausgelöst wird, ist HTTPError - speziell ein 404 für eine Ihrer URLs geworfen wird. Sie könnten entweder ignorieren:

for l in bios:
    OpenThisLink = url + l
    try:
        response = urllib2.urlopen(OpenThisLink)
    except urllib2.HTTPError:
        pass

Oder Sie könnten wieder erhöhen den Fehler mit einer (marginal) sinnvollen Nachricht:

for l in bios:
    OpenThisLink = url + l
    try:
        response = urllib2.urlopen(OpenThisLink)
    except urllib2.HTTPError as e:
        raise Exception('Error opening %s: %s' % (e.geturl(), e))

Andere Tipps

Ich weiß nichts über die speziellen Bibliotheken Sie verwenden. Allerdings sieht das für mich wie ein großen Stack-Trace, die am Ende zu diesen ursprünglichen Fehlern führen:

  

httperror: HTTP-Fehler 404: Not Found

Ich denke, eine der Verbindungen war schlecht und das löste eine Ausnahme, die nicht gefangen wurde.

Edit:. Mit dem „schlechten“ Ich meine die Seite nicht vom Server abgerufen werden kann, daher der Fehler 404

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top