Download from url in python
def download(url, local_path=None):
import urllib2
logger = logging.getLogger()
resp = urllib2.urlopen(url)
content_disp = resp.info().get('Content-Disposition')
if local_path:
local_path = local_path
elif content_disp:
local_path = content_disp.split('filename=')[1].strip('\'\" ')
else:
local_path = os.path.basename(url)
if os.path.exists(local_path):
logger.warning('%s exists, not saved', local_path)
else:
with open(local_path, 'wb') as f:
f.write(resp.read())
logger.info('Saved as %s', local_path)
return local_path