kender kender - 1 year ago 61
Python Question

How to download a file using python in a 'smarter' way?

I need to download several files via http in Python.

The most obvious way to do it is just using urllib2:

import urllib2
u = urllib2.urlopen('')
localFile = open('file.html', 'w')

But I'll have to deal with the URLs that are nasty in some way, say like this:!Run.aspx/someoddtext/somemore?id=121&m=pdf
. When downloaded via the browser, the file has a human-readable name, ie.

Is there any way to handle that in python, so I don't need to know the file names and hardcode them into my script?

Oli Oli
Answer Source

Download scripts like that tend to push a header telling the user-agent what to name the file:

Content-Disposition: attachment; filename="the filename.ext"

If you can grab that header, you can get the proper filename.

There's another thread that has a little bit of code to offer up for Content-Disposition-grabbing.

remotefile = urllib2.urlopen('')['Content-Disposition']