Hit Yahoo Answers when you have trouble with blog material

A script that uses the Yahoo API to hit new entries in Yahoo Answers. As usual, the Unicode escape problem is solved with an external file.

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")

If there is no chie.txt, it will be created, and if there is, it will be overwritten, so I want to eliminate the troublesomeness unless I delete the created chie.txt at the second and subsequent executions.

Recommended Posts

Hit Yahoo Answers when you have trouble with blog material
If you have trouble importing between folders when creating a package with Cython, see this.
Are you having trouble with "pipenv.exceptions.ResolutionFailure"?