Created
February 12, 2013 14:17
-
-
Save zertrin/4770161 to your computer and use it in GitHub Desktop.
Helper function to grab web pages with urllib2. Returns content of the page if retrieval was successful, None otherwise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
def getpage(url): | |
""" | |
Helper function to grab web pages with urllib2 | |
returns content of the page if retrieval was successful, None otherwise | |
""" | |
req = urllib2.Request(url) | |
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0') | |
try: | |
response = urllib2.urlopen(req) | |
except urllib2.URLError as e: | |
if hasattr(e, 'reason'): | |
print 'We failed to reach a server.' | |
print 'Reason: ', e.reason | |
elif hasattr(e, 'code'): | |
print 'The server couldn\'t fulfill the request.' | |
print 'Error code: ', e.code | |
return None | |
else: | |
return response.read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment