AttributeError: 'module' object has no attribute 'urlopen'

delete picture delete · Oct 19, 2010 · Viewed 303k times · Source

I'm trying to use Python to download the HTML source code of a website but I'm receiving this error.

Traceback (most recent call last):  
    File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\DICParser\src\WebDownload.py", line 3, in <module>
     file = urllib.urlopen("http://www.python.org")
AttributeError: 'module' object has no attribute 'urlopen'

I'm following the guide here: http://www.boddie.org.uk/python/HTML.html

import urllib

file = urllib.urlopen("http://www.python.org")
s = file.read()
f.close()

#I'm guessing this would output the html source code?
print(s)

I'm using Python 3.

Answer

eumiro picture eumiro · Oct 19, 2010

This works in Python 2.x.

For Python 3 look in the docs:

import urllib.request

with urllib.request.urlopen("http://www.python.org") as url:
    s = url.read()
    # I'm guessing this would output the html source code ?
    print(s)