What command to use instead of urllib.request.urlretrieve? What command to use instead of urllib.request.urlretrieve? python-3.x python-3.x

What command to use instead of urllib.request.urlretrieve?


Deprecated is one thing, might become deprecated at some point in the future is another.

If it suits your needs, I'd continuing using urlretrieve.

That said, you can do without it:

from urllib.request import urlopenfrom shutil import copyfileobjwith urlopen(my_url) as in_stream, open('my_filename', 'wb') as out_file:    copyfileobj(in_stream, out_file)


requests is really nice for this. There are a few dependencies though to install it. Here is an example.

import requestsr = requests.get('imgurl')with open('pic.jpg','wb') as f:  f.write(r.content)


Another solution without the use of shutil and no other external libraries like requests.

import urllib.requestimage_url = "https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png"response = urllib.request.urlopen(image_url)image = response.read()with open("image.png", "wb") as file:    file.write(image)