实现有道在线翻译

import requests
from bs4 import BeautifulSoup

# 实现有道在线翻译

trans_url = 'http://m.youdao.com/translate'
headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Mobile Safari/537.36'}

try:
    trans_str = input('请输入待翻译内容:')
    data = {'inputtext': trans_str, 'type': 'AUTO'}
    r = requests.post(trans_url, data=data, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')
    ret = soup.find(id="translateResult")
    final_ret = ret.get_text().strip()
    print('翻译结果是:'+ final_ret)
except Exception as e:
    print(e)

 

posted @ 2020-07-08 22:07  程序法  Views(187)  Comments(0Edit  收藏  举报