Save images using python3 requests

Save image using requests

# -*- 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')

reference

https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests

https://torina.top/detail/161/

About urllib.request.urlretrieve

requests (quick start)

shutil documentation page

Recommended Posts

Save images using python3 requests
Download images using requests
Retry post request using python requests
Generating multilingual text images using Python
Start using Python
Log in to Slack using requests in Python
Function to save images by date [python3]
Scraping using Python
[Python] Save images drawn in canvas using Selenium (ActionChains, PyAutoGUI, base64, etc.)
Load images from URLs using Pillow in Python 3
Anonymous upload of images using Imgur API (using Python)
Operate Redmine using Python Redmine
Fibonacci sequence using Python
Data analysis using Python 0
Data cleaning using Python
Using Python #external packages
WiringPi-SPI communication using Python
Age calculation using python
Collect images using icrawler
Search Twitter using Python
Retry with python requests
Name identification using python
Python Requests status code
Notes using Python subprocesses
Try using Tweepy [Python2.7]
Try projective transformation of images using OpenCV with Python
Register a ticket with redmine API using python requests
Write data to KINTONE using the Python requests module
Python notes using perl-ternary operator
Flatten using Python yield from
Scraping using Python 3.5 async / await
[Blender Python] Display images on 3D View using OpenGL (bgl)
Bordering images with python Part 1
Base64 encoding images in Python 3
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Using Quaternion with Python ~ numpy-quaternion ~
Aim python library master (18) requests
Try using Kubernetes Client -Python-
Save images with web scraping
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Post to Twitter using Python
Start to Selenium using python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Disable SSL validation without using verify = False in Python requests
Collect tweets using tweepy in Python and save them in MongoDB
Categorize cat images using ChainerCV
Create JIRA tickets using Python
Instrument control using Python [pyvisa]
Manipulate spreadsheets locally using Python
Python memo using perl --join
Web scraping using Selenium (Python)
[Python] I tried using OpenPose
[Python] JSON validation using Voluptuous
Faster loading of Python images