#!/usr/bin/env/python # -*- coding:utf-8 -*- """ 在线翻译 """ import http.client import hashlib import urllib import random import re import json import requests def trans_baidu(str_text, mod="en2cn"): """ 2022.8月底开始收费了 输入要翻译的文本(单词或句子或文章,但是<6000bytes) :param str_text: :return: """ appid = '20191115000357320' # 填写你的appid secretKey = 'ovn8spgUH6GQ_GhF7V6u' # 填写你的密钥 httpClient = None myurl = '/api/trans/vip/translate' fromLang = 'en' # 原文语种 toLang = 'zh' # 译文语种 salt = random.randint(32768, 65536) q = str_text sign = appid + q + str(salt) + secretKey sign = hashlib.md5(sign.encode()).hexdigest() myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str( salt) + '&sign=' + sign try: httpClient = http.client.HTTPConnection('api.fanyi.baidu.com', timeout=5) httpClient.request('GET', myurl) # response是HTTPResponse对象 response = httpClient.getresponse() result_all = response.read().decode("utf-8") result = json.loads(result_all).get("trans_result")[0]["dst"] # print(result) return result except Exception as e: print(e) return "" finally: if httpClient: httpClient.close() def googleTranslate(text): headers = { 'origin': "https://translate.google.cn", 'referer': "https://translate.google.cn/", 'sec-fetch-dest': "empty", 'sec-fetch-mode': "cors", 'sec-fetch-site': "same-origin", 'x-same-domain': "1", # 'cookie': "NID=511=mJLHCmmZj7H4zcU4YUucXYSlaD67X5HEt4VVVm9Q04ZNr82Hoei_fig0IPjcnpmteHRktufKmckQtyTe6w3GdT0Uk8xTG8F3-ymMn1u6xNiQp4EGlTtFrmXYbCqGw-RJWtM3eQHBGJhxSSxGFCMEZwDuhRAM0bUy1uN2EKdftAI; _ga=GA1.3.126656540.1654615557; OTZ=6538526_24_24__24_; _gid=GA1.3.490013375.1656158367", # 'cookie': "NID=511=mJLHCmmZj7H4zcU4YUucXYSlaD67X5HEt4VVVm9Q04ZNr82Hoei_fig0IPjcnpmteHRktufKmckQtyTe6w3GdT0Uk8xTG8F3-ymMn1u6xNiQp4EGlTtFrmXYbCqGw-RJWtM3eQHBGJhxSSxGFCMEZwDuhRAM0bUy1uN2EKdftAI; _ga=GA1.3.126656540.1654615557; OTZ=6538526_24_24__24_; _gid=GA1.3.490013375.1656158367", # 'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37", # 'user-agent': "'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36", # 'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37", 'user-agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36", 'accept-encoding': "gzip, deflate, br", 'content-type': "application/x-www-form-urlencoded;charset=UTF-8", # 'x-client-data': 'CIW2yQEIpbbJAQjEtskBCKmdygEIrMfKAQj2x8oBCPfHygEItMvKAQihz8oBCNzVygEIi5nLAQjBnMsB', # 'Decoded': 'message ClientVariations {repeated int32 variation_id = [3300101, 3300133, 3300164, 3313321, 3318700, 3318774, 3318775, 3319220, 3319713, 3320540, 3329163, 3329601];}', # 'x-goog-batchexecute-bgr':'[";qrS4tPnQAAYvqchvD6lftQckgqu-IikmACkAIwj8Rl2XS1QudgthqdZU0SqH0vq1-Bet3w6a53VSL12iqjefO-AFZh8AAAD0TwAAAAZ1AQcXAM87g0YlzGEpU91FDlXisD9zIJjkeYnnWCmMotddHWrZBkIpzy46jxKdAuo3aVXneE15f0O10ZpS9nTBSJdcScQqzGcI93uNpDbDtSbwr8Xz67U4XIz5FW3C9I5y7bOK5pX3nMy3dad3_gdoicKNjO1U7ey1NqdWArjc5PE6uCn3YnoiN-YUhlwK-j702Vm8nz-6B4oKWr-e3NSr2nkrHLBU4WZ9AmIg4CpGGGfk8Ri2aTL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VDBGfAZwvR3YpljVZMeO6IUNBDwy5wDMh8AAABSTwAAAAJ1AQcXAIlC1pgskV32-yOzV6FmYVKrbi30KaxqJklCs1zPpGHX_hLbfLToBJPYoWuAZ_EDLf25YVbCAKmrwVah78G7syapqN1EYiNKZfShvZp60vbhcqeJWm7zwznMjhKjfdKuTq5Rq41JelMT_EQVGypnsHBqTderknEjPtt6TmIMIJjWRfjZsUVvRnde3oQCTLCuZUBVv3eTx3oT7W-EyIImn9XZEs8AKxQKK2vZRRc6VkveQMHi5DqyNT8YgqSntvAGOLImK402fdeua07F-nXkiiULeaJJ-7vjCMM-3jJNgEO6Vw6UJ6U74c_esPqwbtehb8dAO1uv_Lv9WJDX0KnHNkSRrsdD1z0lqxCQBZOxUpDGLc4Cvz_1FmWBU3Hy9RXuT_NW149LFR5JG6TgRQchengsqAUaC48H1vejyaG9Q6MDJdbWdnQxFtcNjIJDM7fOvVlsHzcNcCW11uP4AA55e28eCrbIi3ZeVRSRpWhWfRvoWg8Yzp1dIwnDS6vlnjr1UwZ6wLTfqF5ho0kSe1qcwza73nSZvi73NeyeaSkbsnO25bi4Jzp5sronwfrOqQALcgYWKX-krf6j-ug17IQm278z-sP4dJy3s4bZyWndiF5wO9tlJ7vZ_GRDiUVAZUNho_9WGi9oB89WvzjzqDMxvlx1WV_SonpzVaX9WZX0oIoDEs0FqRW165sinSZGAk63gejcPbH9ADj1xYQpP6YrS_iYNpabpSR544jwXT0V-f8bGf-bOBMViidcwgW0UloFN8i9v7ppuOxXbrp83H7Q8Lh5Lw0YCWijHaZ_JC3w-FZOa_j66ujYsxCF8t5cVB_O2Ttnks1eRYutJwg7a7nKvhDv24N_CPPvgn4bLG8WzEs_av9QGIfwGt1_ADJ4f8snG9rMsVOXzL7FMtV4b5Q9olCSi-CsXhwqxWIxDs9xdiolrpKKupfK89U6x13vv8kiq0Y5qnU9Un6AuQ",null,null,3,null,null,null,0,"2"]', } # url = "https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&source-path=/&f.sid=-127429216746380775&bl=boq_translate-webserver_20220622.08_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=882057&rt=c" # url = "https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute?rpcids=rpcids=MkEWBc&f.sid=-2984828793698248690&bl=boq_translate-webserver_20201221.17_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=5445720&rt=c" # url = "https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&source-path=/&f.sid=5723009767851758792&bl=boq_translate-webserver_20220622.08_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=66251&rt=c" url = "https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&source-path=/&f.sid=424574510049276369&bl=boq_translate-webserver_20220803.08_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=1235460&rt=c" # data = {'f.req': f'[[["MkEWBc","[[\\"{text}\\",\\"zh-CN\\",\\"en\\",true],[null]]",null,"generic"]]]'} data = {'f.req': f'[[["MkEWBc","[[\\"{text}\\",\\"en\\",\\"zh-CN\\",true],[null]]",null,"generic"]]]'} means = [] try: res=requests.post(url, headers=headers, data=data, timeout=5).text print(res) pattern = '\)\]\}\'\s*\d{3,4}\s*\[(.*)\s*' part1 = re.findall(pattern, res) part1_list = json.loads('[' + part1[0])[0] if part1_list[2] is None: # print(text) return text content1 = part1_list[2].replace('\n', '') content1 = json.loads(content1) print(content1) if len(content1) > 3 and content1[3]: if len(content1[3]) > 5: specific_means = content1[3][5:] # print("specific_means:", specific_means) if specific_means[0]: part2_list = specific_means[0][0] means = [j[0] for i in part2_list for j in i[1]] # i[0]是词性 return ";".join(means) part2_list = content1[1][0][0][5:][0] means = [i[0] for i in part2_list] print(means) return ";".join(means) except: return "" def translate_youdao(word=None): """ 结果不全 :param word: :return: """ url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' # url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' Form_data = { 'i': word, 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': '1528199964615', 'sign': 'f6cf55466c876c404ff85ea3fc8c453f', 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_REALTIME', 'typoResult': 'false' } try: response = requests.post(url=url, data=Form_data, timeout=5) content = json.loads(response.text) print(content) # print(content['translateResult'][0][0]['tgt']) return content['translateResult'][0][0]['tgt'] except: return "" def youdaoTranslate(text): """ 通过html标签获取 :param text: :return: """ r = requests.get("http://dict.youdao.com/w/eng/{}/#keyfrom=dict2.top.suggest".format(text)) all_content = r.content.decode('utf-8') trans_container = re.findall(r'