how to download a file python code example

Example 1: python download file from url

import requests


url = 'https://www.facebook.com/favicon.ico'
r = requests.get(url, allow_redirects=True)

open('facebook.ico', 'wb').write(r.content)

Example 2: how to download file from python

import wget

url = "https://www.python.org/static/img/[email protected]"

wget.download(url, 'c:/users/LikeGeeks/downloads/pythonLogo.png')

Example 3: how to download file in python

import urllib2

filedata = urllib2.urlopen('http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg')
datatowrite = filedata.read()
 
with open('/Users/scott/Downloads/cat2.jpg', 'wb') as f:
    f.write(datatowrite)