Created
December 7, 2016 23:52
-
-
Save uni8inu/a3d7494392be7a98747aeb33b874a7e7 to your computer and use it in GitHub Desktop.
なろうディベロッパー Python3版サンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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