# -*- coding: utf-8 -*-
import requests
import shutil
def download_img(url, file_name):
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(file_name, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
if __name__ == '__main__':
download_img('Write the URL here', 'Write the name of the file you want to save')
I received an edit request with the following content, so I will post it together. (Slight changes have been made.) This may be smarter.
# -*- coding: utf-8 -*-
import requests
def download_img(url, file_name):
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(file_name, 'wb') as f:
f.write(r.content)
if __name__ == '__main__':
download_img('Write the URL here', 'Write the name of the file you want to save')
https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests
https://torina.top/detail/161/
About urllib.request.urlretrieve
Recommended Posts