LoginSignup
2
2

More than 5 years have passed since last update.

ブログネタに困ったときにYahoo知恵袋を叩く

Posted at

Yahoo APIを使ってYahoo知恵袋のエントリー新着を叩くだけのスクリプト。
例によってUnicodeエスケープ問題を外部ファイルで解決する甘え。

chie.py

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import requests
import json,urllib2
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

url = "http://chiebukuro.yahooapis.jp/Chiebukuro/V1/getNewQuestionList?"
appid = "&appid=YoueID"
output = "&condition=open&output=json"
req_seq = url + appid + output

r = requests.get(req_seq)
res = r.json()

lists = json.dumps(res, sort_keys=True, indent=4)

f = open("chie.txt", "aw")
for x in lists:
    f.write(str(x))
    f.close

f = open("chie.txt", "rb")
data = f.read()
f.close()
print data.decode("unicode-escape")

chie.txtがなければ作る、あれば上書きされるので2回目以降の実行のときは作られたchie.txtを削除しないと面倒なことになることを解消したい。

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