Help us understand the problem. What is going on with this article?

python3のrequestsを使って画像を保存

More than 1 year has passed since last update.

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('ここにURLを書く', '保存したいファイル名を書く')

以下のような内容の編集リクエストを頂いたので合わせて載せておきます。(若干変更を加えました。)
こっちのほうがスマートかもしれません。

# -*- 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('ここにURLを書く', '保存したいファイル名を書く')

参考

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

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

urllib.request.urlretrieveについて

requests(クイックスタート)

shutilのドキュメントページ

utvirtual
VRの普及啓蒙と体験創造を理念として、VR作品の制作・展示、勉強会などを行っているインカレサークル
http://utvirtual.tech/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした