在命令行用使用有道翻译,python写一个命令行有道词典
来源:互联网 发布:手机json格式怎么打开 编辑:程序博客网 时间:2024/05/16 23:35
平常都是用终端敲, 有时候不会的词语也懒得打开词典了,干脆搞了个简单的查词命令.思路也很简单,直接调用有道的api,解析下返回的json就ok了。只用到了python原生的库,支持python2和python3.
#!/usr/bin/env python# -*- coding:utf-8 -*-# API key:273646050# keyfrom:11pegasus11import jsonimport systry: # py3 from urllib.parse import urlparse, quote, urlencode, unquote from urllib.request import urlopenexcept: # py2 from urllib import urlencode, quote, unquote from urllib2 import urlopendef fetch(query_str=''): query_str = query_str.strip("'").strip('"').strip() if not query_str: query_str = 'python' print(query_str) query = { 'q': query_str } url = 'http://fanyi.youdao.com/openapi.do?keyfrom=11pegasus11&key=273646050&type=data&doctype=json&version=1.1&' + urlencode(query) response = urlopen(url, timeout=3) html = response.read().decode('utf-8') return htmldef parse(html): d = json.loads(html) try: if d.get('errorCode') == 0: explains = d.get('basic').get('explains') for i in explains: print(i) else: print('无法翻译') except: print('翻译出错,请输入合法单词')def main(): try: s = sys.argv[1] except IndexError: s = 'python' parse(fetch(s))if __name__ == '__main__': main()
使用
- 将上面代码粘贴后命名为youdao.py
- 修改名称
mv youdao.py youdao
, 然后加上可执行权限chmod a+x youdao
- 拷贝到/usr/local/bin。
cp youdao /usr/local/bin
使用的时候把要翻译的单词作为第一个命令行参数,要是句子用引号括起来。
原文链接:http://ningning.today/2015/12/02/python/%E7%94%A8python%E5%86%99%E4%B8%80%E4%B8%AA%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%9C%89%E9%81%93%E8%AF%8D%E5%85%B8/
1 0
- 在命令行用使用有道翻译,python写一个命令行有道词典
- 用python写一个命令行有道翻译
- Python实现的命令行查询有道词典
- linux命令行版有道词典
- Linux命令行有道词典查询
- 有道词典工具(命令行+GUI)
- Python 实现有道翻译命令行版
- 有道词典Python模拟翻译
- [python爬虫]--调用有道词典进行翻译
- 有道翻译词典
- Python有道词典
- 连接有道网页仿写有道词典
- 在ubuntu下使用有道词典
- 有道词典的使用
- 命令行实现有道英汉字典
- python有道翻译
- Python实现有道翻译
- Python:有道翻译
- php分页编写
- [JSOI2008]星球大战
- 我对指针的误解
- ScrollView嵌套ListView只显示一行
- poj 1703 Find them, Catch them
- 在命令行用使用有道翻译,python写一个命令行有道词典
- 从安装系统到oracle数据库生产环境(centos6.8)搭建-完全版
- ncurses--读书笔记3
- 项目经理--新接项目--思维(一)
- Leetcode001--两数之和为指定数
- 9.18目标
- Boost.Any
- JFreeChart简单开发
- Android studio 打jar包 方法总结