本文主要是介绍linux终端添加有道翻译,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
方法一:直接使用第三方库
安装(https://github.com/longcw/youdao)
sudo pip install youdao
方法二:直接使用脚本
1.获取有道翻译脚本,cat youdao.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-# API key:273646050
# keyfrom:11pegasus11import json
import systry: # py3from urllib.parse import urlparse, quote, urlencode, unquotefrom urllib.request import urlopen
except: # py2from urllib import urlencode, quote, unquotefrom 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()
2.改成可执行文件
cp youdao.py yd
chmod a+x yd
sudo cp yd /usr/local/bin
参考
- http://blog.csdn.net/lina_acm/article/details/52573400
这篇关于linux终端添加有道翻译的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!