码迷,mamicode.com
首页 > 编程语言 > 详细

使用百度翻译开放API构建的python命令行词典

时间:2014-12-13 06:05:41      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   color   使用   sp   for   on   

不多说,上代码:

 1 #!/usr/bin/python
 2 #-*- encoding=utf-8 -*-
 3 
 4 import urllib
 5 import urllib2
 6 import json
 7 
 8 keys=raw_input(‘input you word > ‘)
 9 
10 args={‘q‘:keys,‘from‘:‘en‘,‘to‘:‘zh‘}
11 
12 encode_args=urllib.urlencode(args)
13 
14 url=‘http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id=这里添加你的API-KEY&‘+encode_args
15 print url
16 
17 json_encode=urllib2.urlopen(url).read()
18 json_decode=json.loads(json_encode)
19 
20 symbols=json_decode[‘data‘][‘symbols‘]
21 
22 print ‘您输入的关键词是:‘,json_decode[‘data‘][‘word_name‘]  #输出搜索关键词
23 
24 for index in symbols:           #将每个symbols解析
25     for every in index[‘parts‘]:    #对每个部分的parts解析
26         print ‘词性:‘,every[‘part‘]
27         print ‘释义:‘
28         for i in every[‘means‘]:    #对每个部分的含义进行解析
29             print ‘|-‘,i
30     print ‘美音‘,index[‘ph_am‘]
31     print ‘英音‘,index[‘ph_en‘]

 

暂时不支持中文查询,因为没有找到怎么判断中文输入。

 

以上。

使用百度翻译开放API构建的python命令行词典

标签:style   blog   http   ar   color   使用   sp   for   on   

原文地址:http://www.cnblogs.com/lhyz/p/4160808.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!