tags:

views:

64

answers:

1

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
Tried that and I got "AttributeError: 'module' object has no attribute 'urlopen'"
BiscottiGummyBears
Actually, the correct module to import is urllib2.
Davide Gualano
Sorry, I just noticed that you were using Python 3. I've updated my example to match.
Greg Hewgill
Awesome, thanks a lot.
BiscottiGummyBears
@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
@Greg: my bad, I didn't read the question title carefully enough :)
Davide Gualano