[Tutor] Download file from the web and store it locally.
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon May 22 20:29:50 CEST 2006
On Mon, 22 May 2006, MATATA EMMANUEL wrote:
> import urllib
> import sys
> # Get a file-like object from the web and store it locally.
> url = "http://www.co.rowan.nc.us/enviroservs/downloads.htm"
> f = urllib.urlopen(url)
> # connect to the remote
> try:
> remote = urllib.urlopen(url)
> except:
> print "Connot open URL"
> sys.exit()
Don't do this kind of exception handling: it hides information about the
exception.
If something bad happens, we want to see something other than "Cannot open
URL". So instead, let the exception fall through:
###########################################################
url = "http://www.co.rowan.nc.us/enviroservs/downloads.htm"
f = urllib.urlopen(url)
remote = urllib.urlopen(url)
###########################################################
Or at the very least, modify the exception handler to print out more
informative error messages other than "An error happened". We can use the
'traceback' module in the Standard Library to get us more informative
error messages.
While you're writing simple programs, I'd recommend avoiding exception
handlers, just because if something bad happens, we need to preserve all
the debugging information we can get.
> # Read from the object, storing the page's contents in 's' --> want to
> download parcels.zip.
> s = f.read(Parcels.zip)
What is parcels.zip? I don't understand what this is trying to do yet.
At this point, 'f' is a file whose contents contains the stuff in
http://www.co.rowan.nc.us/enviroservs/downloads.htm
I get the feeling you're thinking like a web browser, in the sense that
you need to first visit the page before downloading the file.
If so, that's not it. *grin*
If the file you're trying to download has a URL like:
http://www.co.rowan.nc.us/enviroservs/Parcels.zip
then there is no reason to have Python visit the downloads.htm page first:
you can go ahead and grab Parcels.zip first, unless the content provider
has done something weird to their web server.
One other comment:
######
m = urllib.urlretrieve
######
This won't fire off. In Python, functions that take no arguments still
need parens to be called.
More information about the Tutor
mailing list