Linux 翻译小工具

1-shell文件   t7 (翻译句子)

#!/bin/bash

/usr/bin/python <<-EOF
import urllib.request, urllib.parse as parse,json
def youdao(value):
    request_url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    form_Data = {'i': value, 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'doctype': 'json', 'version': '2.1',
                 'keyfrom': 'fanyi.web', 'typoResult': 'false', 'client': 'fanyideskweb'}

    data = parse.urlencode(form_Data).encode('utf-8')

    trans_res = json.loads(urllib.request.urlopen(request_url, data).read().decode('utf-8'))

    show_label = trans_res['translateResult'][0][0]['tgt'] if trans_res.get('translateResult') else ''
    return show_label
print("---------------------------")
print("")
print(youdao("${*}")) # 避免$1需要在待翻译句子两边添加双引号的麻烦
print("")
print("---------------------------")
EOF

2-shell文件   t1 (翻译单词)

#tips:pip(BeautifulSoup4)

#!/bin/bash
/usr/bin/python <<-EOF
import urllib.request, urllib.parse as parse,json
from bs4 import BeautifulSoup 
try:
    url = "https://dict.youdao.com/w/eng/${1}/"
    print("---------------------------")
    print("")
    response = urllib.request.urlopen(url)
    content = response.read().decode('utf-8')
    soup = BeautifulSoup(content, 'html.parser')
    kk=soup.find_all('div',class_='trans-container')
    a_T_end=""
    for k in kk[0].find_all('li'):
          a_T= ''.join(k)
          print(a_T)
except:
     print("")
print("")
print("---------------------------")
EOF

3-制作命令 

  1. 准备标准shell文件去掉 .sh后缀
  2. 复制 shell文件到/usr/bin目录下
  3. chmod 777 [shell文件]

2022-5-9 桃坞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值