var hateblo

毎日書く備忘録

【Py】ネットから日本語 json 取得して保存

requestsをインスコ

sudo pip install requests

Requests の使い方 (Python Library) - Qiita

日本語jsonをtextにdumps

引数にensure_ascii=Falseを設定

json.dumps(jsonfile, sort_keys=True, ensure_ascii=False, indent=2)

pythonでjsonを扱う時、日本語をエスケープさせない方法 - Qiita

ファイル書き込み

with open ('ファイル名','w') as ファイル変数名  
  ファイル変数名.write('text')  

Pythonのwithステートメントのまとめ - $ cat /var/log/shin

import requests
import json

res = requests.get('http'//jsonが取得できるpath').json()
text = json.dumps(res, sort_keys=True, ensure_ascii=False, indent=2)
with open("res.json", "w") as file:
    file.write(text.encode("utf-8"))