2
2

Python requestsで画像ファイルをダウンロード

Posted at

pythonのwebスクレイピングで、結局requestsでいいことが判明したので書いていく。

やり方

テキストや、画像はそれぞれurlを持っている。そいつをrequest.get()にわたすと、データを返してくれるので、それをファイルに書き込めばおkらしい。

コード

websc.py
import requests

tag=画像のurl
responce=requests.get(tag)

with open('newfile','wb') as savefile:
    savefile.write(responce.content) 

.contentがついている意味は自分でもわかっていない。
画像のurlは、右クリックで、「画像のリンクをコピー」でわかる。
書き込まれたファイルのファイル拡張子を画像のものに変更して開けば、ダウンロードしたやつが見られる。

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2