LoginSignup
3
2

More than 5 years have passed since last update.

はてなキーワードAPIで躓きました

Last updated at Posted at 2015-03-23

はてなキーワードAPIで躓きました。
スクリプト自体は問題なく動きますので、ユニコード処理だけ上手くいけばいいんじゃないかと。

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

get_input = raw_input("prease keywords: ")


server = xmlrpclib.ServerProxy("http://d.hatena.ne.jp/xmlrpc")
res = server.hatena.getSimilarWord({"wordlist": get_input})
print res["wordlist"]

実行すると拾いはするんですが、ユニコード処理に問題が。
表示される文字列にユニコードがそのまま出ちゃうのです。

このままでもまぁ、関連キーワードを拾うくらいなら・・・。
勉強不足を実感したので覚書にと。

*******************************************

とりあえずユニコードエスケープをどうにかしたらなんとかなるんじゃないかと発想し、
一旦外部にtxtで保存して読み込んでみました。

hatenaapi.py
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import xmlrpclib
import sys, codecs
sys.stdin  = codecs.getreader("utf-8")(sys.stdin)
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

get_input = raw_input("prease keywords: ")

server = xmlrpclib.ServerProxy("http://d.hatena.ne.jp/xmlrpc")
res = server.hatena.getSimilarWord({"wordlist": get_input})


f = open("hatena.txt" , "aw")
lists = res["wordlist"]

for x in lists:
    f.write(str(x) + "\n")
    f.close


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

もっと簡単にできないもんですかね・・・。

3
2
2

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