views:

48

answers:

1

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 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, thanks for the help!

+3  A: 

This works in Python 2.x.

For Python 3 look here:

http://docs.python.org/py3k/library/urllib.request.html?highlight=urllib#urllib.request.urlopen

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)
eumiro
Hi Eumiro, using the 'with' statement in Python I'm guessing it closes the connection automatically once it's done using it? Similar to a use statement in C#?
Serg
@Sergio: exactly! And through the indentation you see where your file is still opened.
eumiro
@eumiro: Thanks for the help
Serg