方法一:直接使用第三方库
安装(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

这里写图片描述

参考
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐