Created
October 23, 2013 07:49
-
-
Save moxuse/7114253 to your computer and use it in GitHub Desktop.
YAHoo API KeyphraseService
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
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
import optparse | |
import httplib | |
from lxml import etree | |
# Yahoo!API config | |
appId = 'APPLICATION ID' | |
session = httplib.HTTPConnection('jlp.yahooapis.jp') | |
analiseStr = '夏が終わってしまう、鬱だ' | |
# set thresould if you want | |
thresould = 0 | |
def request(reqstStr): | |
analiseStr = reqstStr | |
session.request('GET', '/KeyphraseService/V1/extract?appid='+appId+'&sentence='+analiseStr) | |
response = session.getresponse() | |
if response.status == httplib.OK: | |
data = response.read() | |
# make etree from deta | |
root = etree.fromstring(data) | |
# parse XML | |
if len(root) > 0: | |
for item in root: | |
if int(item[1].text) > thresould: | |
print"result: " + item[0].text + " point: " + item[1].text | |
def main(): | |
parser = optparse.OptionParser('Usage: %prog [options]') | |
parser.add_option('-r', '--request', | |
dest='reqstStr', | |
help='request with strings') | |
(options, args) = parser.parse_args() | |
if len(args) != 0: | |
parser.error('too many arguments') | |
if options.reqstStr: | |
request(options.reqstStr) | |
else: | |
request( analiseStr ) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment