Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
なろうディベロッパー 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment