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
·
方法一:直接使用第三方库
安装(https://github.com/longcw/youdao)
sudo pip install youdao
方法二:直接使用脚本
1.获取有道翻译脚本,cat youdao.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# API key:273646050
# keyfrom:11pegasus11
import json
import sys
try: # py3
from urllib.parse import urlparse, quote, urlencode, unquote
from urllib.request import urlopen
except: # py2
from urllib import urlencode, quote, unquote
from urllib2 import urlopen
def 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 html
def 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
参考
更多推荐
已为社区贡献2条内容
所有评论(0)