1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import requests import execjs
def get_sign(word): with open('test.js', 'r', encoding='utf8') as js: js_code = js.read() sign = execjs.compile(js_code).call('e', word) return sign
def request(word): if word[0] in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']: f = 'en' t = 'zh' else: f = 'zh' t = 'en' sign = get_sign(word) url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh' headers = { 'cookie': '你的cookie', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75 ' } formData = { 'from': f, 'to': t, 'query': word, 'transtype': 'realtime', 'simple_means_flag': '3', 'sign': sign, 'token': '你的token', 'domain': 'common' } response = requests.post(url=url, headers=headers, data=formData).json() if response.get('liju_result').get('tag'): print(', '.join(response.get('liju_result').get('tag'))) if response.get('trans_result').get('data'): for d in response.get('trans_result').get('data'): print(d['dst'])
if __name__ == '__main__': while True: word = input('请输入需要翻译的英文(中文):') if word == 'exit0': break request(word)
|