0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonを使ってRestAPIでファイルを取得する

Posted at

#はじめに
pythonでRestAPIを実行し、テキストファイルを取得し、保存するコードです。

######環境
python3.9.6

#サンプルコード

import requests

url = 'https://[対象のURL]'
headers = {'Content-Type':'application/json','xxx-key':'<API認証キーなど>'}

try:
   #APIで隔離メールをダウンロード、失敗したらエラーを出力
   response = requests.get(url , headers=headers)
   response.raise_for_status()
except requests.exceptions.RequestsException as e:
   print("エラー:",e)

#ファイルを取得できた場合に、filename.txtとしてファイルを保存する。
if response.status_code == 200:
   with open("filename.txt" ,mode='wb') as file:
      file.write(response.content)
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?