views:

254

answers:

1

Here's my code:

from urllib.request import urlopen

response = urllib.urlopen("http://www.google.com")
html = response.read()
print(html)

Any help?

+1  A: 

As stated in the urllib2 documentation at http://docs.python.org/library/urllib2.html:

The urllib2 module has been split across several modules in Python 3.0 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to 3

So you should instead be saying

from urllib.request import urlopen
html = urlopen("http://www.google.com/")
print(html)

Your current, now-edited code sample is incorrect because you are saying urllib.urlopen("http://www.google.com/") instead of just urlopen("http://www.google.com/").

Eli Courtwright
Still getting an error, please see edit. Edit: Still getting an error when using from urllib.request
Serg
@Sergio: It's `urllib.request` and not `urllib2.request`. The `urllib` and `urllib2` modules from Python 2.x have been combined into the `urllib` module in Python 3.
Eli Courtwright