Ich mache gerade ein Web-Scraping mit BeautifulSoup und mir läuft ein seltsamer Fehler auf. Code:BeautifulSoup Timing auf Instanziierung?
print "Running urllib2"
g = urllib2.urlopen(link + "about", timeout=5)
print "Finished urllib2"
about_soup = BeautifulSoup(g, 'lxml')
Hier ist der Ausgang:
Running urllib2
Finished urllib2
Error
Traceback (most recent call last):
File "/Users/pspieker/Documents/projects/ThePyStrikesBack/tests/TestSpringerOpenScraper.py", line 10, in test_strip_chars
for row in self.instance.get_entries():
File "/Users/pspieker/Documents/projects/ThePyStrikesBack/src/JournalScrapers.py", line 304, in get_entries
about_soup = BeautifulSoup(g, 'lxml')
File "/Users/pspieker/.virtualenvs/thepystrikesback/lib/python2.7/site-packages/bs4/__init__.py", line 175, in __init__
markup = markup.read()
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 355, in read
data = self._sock.recv(rbufsize)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 588, in read
return self._read_chunked(amt)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 648, in _read_chunked
value.append(self._safe_read(amt))
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 703, in _safe_read
chunk = self.fp.read(min(amt, MAXAMOUNT))
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 384, in read
data = self._sock.recv(left)
timeout: timed out
Ich verstehe, dass die urllib2.urlopen
Probleme verursachen könnte, aber die Ausnahme tritt in der Leitung Instanziieren BeautifulSoup. Ich habe etwas gegoogelt, konnte aber nichts über BeautfiulSoup
Timeout-Probleme finden.
Irgendwelche Ideen, was passiert?
Huh, so das 'urllib2.urlopen' Objekt keine Ausnahme ausgelöst, wenn instanziiert? – user2740614
@ user2740614 nein, es würde ausgelöst werden, wenn 'read()' darauf aufgerufen wird. – alecxe
Okay, das erklärt, warum es nicht funktioniert. Wenn es Ihnen nichts ausmacht, warum würden Sie 'urllib2.urlopen' so gestalten? Möchten Sie nicht, dass es schneller versagt (d. H. Bei Instanziierung)? Nur neugierig :) – user2740614