【Python3】なろう小説APIサンプルプログラムのPython版
なろうディベロッパーのphpサンプルコードをPython3で書きました。
Python3版
import json from urllib.request import urlopen import gzip #sample get #refer to: http://dev.syosetu.com/man/sample01/ #APIのURL(パラメーターを指定してください) url = "http://api.syosetu.com/novelapi/api/?out=json&lim=100&gzip=5" #APIを取得 response = urlopen(url) #解凍する with gzip.open(response,"rt",encoding="utf-8") as f: j_raw = f.read() # caution : memory over error #JSONデコード jObj = json.loads(j_raw) #jObj0番目はallcountなので1番目から処理 for a_novel in jObj[1:]: #titleの表示 title = a_novel['title'] print(title) #あらすじの表示 story = a_novel['story'] print(story) #小説へのリンク作成 ncode = a_novel['ncode'] link = "http://ncode.syosetu.com/{}/".format(ncode.lower()) print(link)