How can I get python to get the contents of an HTTP page? So far all I have is the request and I have imported http.client.
+6
A:
Using urllib.request
is probably the easiest way to do this:
import urllib.request
f = urllib.request.urlopen("http://stackoverflow.com")
print(f.read())
Greg Hewgill
2010-01-07 21:48:08
Tried that and I got "AttributeError: 'module' object has no attribute 'urlopen'"
BiscottiGummyBears
2010-01-07 21:52:02
Actually, the correct module to import is urllib2.
Davide Gualano
2010-01-07 21:53:07
Sorry, I just noticed that you were using Python 3. I've updated my example to match.
Greg Hewgill
2010-01-07 21:53:31
Awesome, thanks a lot.
BiscottiGummyBears
2010-01-07 21:58:39
@Davide Gualano: The Python 2.x `urllib2` module has been rolled into the Python 3.x `urllib` set of modules: http://docs.python.org/library/urllib2.html
Greg Hewgill
2010-01-07 21:58:50
@Greg: my bad, I didn't read the question title carefully enough :)
Davide Gualano
2010-01-07 22:08:33