[Tutor] python2 vs python3 urllib

pierre dagenais eq742 at ncf.ca
Sun Apr 24 00:58:03 CEST 2011


The following code works as expected with python version 2.6.5, but with 
version 3.1.2 I get the following error:

pierre:/MyCode/mesProjets$ py3 test.py

Traceback (most recent call last):
   File "test.py", line 3, in <module>
     sock = urllib.urlopen("http://diveintopython.org/")
AttributeError: 'module' object has no attribute 'urlopen'




The code:

#example 8.5 of diveintopython.org/
import urllib
sock = urllib.urlopen("http://diveintopython.org/")
htmlSource = sock.read()
sock.close()
print (htmlSource)

What is the proper syntax in version 3?

Thank you,
Your help is much appreciated.


More information about the Tutor mailing list