#!/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'
\s*', all_content.replace(" ", " ").replace("\t", ""), flags=re.S) # print(trans_container) if trans_container: trans = [i for i in re.split(r"", trans_container[0]) if i.strip() and re.search( "[((]\s*人名\s*[))]", i) is None] # 人名的翻译不需要 return ";#;" .join(trans) return "" def haiciTranslate(text): """ 通过html标签获取 :param text: :return: """ r = requests.get("https://dict.cn/search?q={}".format(text)) all_content = r.content.decode('utf-8') # print(all_content) dict_basic = re.findall(r'', all_content.replace(" ", " ").replace("\t", ""), flags=re.S) if not dict_basic: dict_basic = re.findall(r'
[\n\s]*(.+?)', all_content.replace(" ", " ").replace("\t", ""), flags=re.S) if dict_basic: trans = [re.sub("|", "", i).replace("\n", "") for i in re.findall(r"
  • (.+?)
  • ", dict_basic[0], flags=re.S) if i.strip() and re.search( "[((]\s*人名\s*[))]", i) is None] return ";#;".join(trans) return "" def haici_zh2en(text): r = requests.get("https://dict.cn/search?q={}".format(text), timeout=0.5) all_content = r.content.decode('utf-8') # print(all_content) dict_basic = re.findall(r'
    (.+?)
    ', all_content.replace(" ", " ").replace("\t", ""), flags=re.S) if dict_basic: en_list = re.findall('(.+?)', dict_basic[0], flags=re.S) en_list = [en.strip() for en in en_list] # print(en_list) return en_list def baiduTranslate(text): """ 通过html标签获取 :param text: :return: """ # url = "https://fanyi.baidu.com/translate?aldtype=16047&query=break+down&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/break%20down" # url = "https://fanyi.baidu.com/#en/zh/break%20down" url = "https://fanyi.baidu.com/sug" Form_data = { 'kw': text, } response = requests.post(url=url, data=Form_data, timeout=5) content = json.loads(response.text) print(content) def en2ch_baidu(text): """ 百度翻译 :return: """ url = "https://fanyi.baidu.com/transapi" Form_data = { 'query': text, # .replace(" ", "+") 'from': "en", 'to': "zh", 'source': "txt" } headers = { # "Acs-Token": "1662274986285_1662362866177_PZ9RGDfoZT611QxcCOPF8OFKwuWbo2SxPixIRvRUghgD6AKWj+XA4XpEy7jAsAxs3jxn/CnuUNVYI3E2GZI/pkKZt9XbuQOuHUVVAf6WjA7RfsNhNa0Va1Rr9au9Fskav7ZLIVmTCJ+cFFnstBktKqO8OFOYHfdVgXFcqjPzmz34yuMe/0xH8FmAibeLtkQE/OHK4rJ18afENGyeGlFpY1R/mQUCfROo1CjZ1wk9yhUFxFZmgToyLDDuIfngU3HPylJZtk6LmV20vbSS8hA+7vJk4XPppOLRI/+Uapoks//SxVm2OtpMlmM4zGuQ82N9YBaQy3nS2NjbYy08hnNI1rsPDikdQI0tBawhm91wsrgMsdXaRSfkEqy7ZpAOMH0ebMYck5r4l+OSXe7lR3XoA4CZU6Rd+eaQ9xHktcdtaKA=", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Cookie": 'PSTM=1646122173; BIDUPSID=4A46D96DC8423B1990661EC67CB17B97; __yjs_duid=1_5eebbceeddec85cf72e17b95994355071646122377960; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; APPGUIDE_10_0_2=1; BAIDUID=68FE11EE3BE9ADA190124B58EB36B628:FG=1; MCITY=-289%3A; H_PS_PSSID=; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1666148184,1666156674,1666168716,1666248882; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1666248882; BAIDUID_BFESS=68FE11EE3BE9ADA190124B58EB36B628:FG=1; delPer=0; PSINO=5; BA_HECTOR=218l21alahak2g00ak0k45e51hl268u1a; ZFY=tzTetLMt:AQKpcqHBdtdKVRVB8GNVB7XLc:BQRFb:BJvjQ:C; BDRCVFR[b3Q6T2w4uHD]=qdOo65HpBYTTh7znWc8mvqV; ab_sr=1.0.1_NWU3MmM0NWQyOWQxZmY5ZTBhZDNhODcwNTBmN2JkMjM3YmVjZWFlNzE2ODJmZWFjMzNhNWQwM2RmZDc4NGQ4ODJiMTVhMGUxZWM4YzkzMmI2ZDY2N2YzMWY0MWFkOWZkMjFkNzQ0YTc1NmFjOWJhNzRmZjVlZjUwZmNmNjc1ZTlhNzYxYTEzODkxY2NiZWMzOGVmMGIwMWFlYmNjNzhlYg==', "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36", } # url = "https://fanyi.baidu.com/translate?aldtype=16047&query=break+down&keyfrom=baidu&smartresult=dict&lang=auto2zh" response = requests.post(url=url, data=Form_data, headers=headers, timeout=5) # re = eval("u" + "\'" + response.text + "\'") # aa = response.content.decode('unicode_escape') content = json.loads(response.content) print(content) # try: if "result" in content: res = eval(content["result"].replace("null", "\"\""))["content"] mean_list = [] for i in res[0]['mean']: # print(i) ptag = i["pre"] if "pre" in i else "" mean = list(i["cont"].keys()) mean_list.append(ptag + ";".join(mean)) return "\n".join(mean_list) if "data" in content: mean_list = [] for i in content['data']: # print(i['dst']) mean_list.append(i['dst']) return "\n".join(mean_list) # except: # return "" def ch2en_baidu(text): url = "https://fanyi.baidu.com/transapi" Form_data = { 'query': text.replace(" ", "+"), 'from': "zh", 'to': "en", 'source': "txt" } headers = { # "Acs-Token": "1662274986285_1662362866177_PZ9RGDfoZT611QxcCOPF8OFKwuWbo2SxPixIRvRUghgD6AKWj+XA4XpEy7jAsAxs3jxn/CnuUNVYI3E2GZI/pkKZt9XbuQOuHUVVAf6WjA7RfsNhNa0Va1Rr9au9Fskav7ZLIVmTCJ+cFFnstBktKqO8OFOYHfdVgXFcqjPzmz34yuMe/0xH8FmAibeLtkQE/OHK4rJ18afENGyeGlFpY1R/mQUCfROo1CjZ1wk9yhUFxFZmgToyLDDuIfngU3HPylJZtk6LmV20vbSS8hA+7vJk4XPppOLRI/+Uapoks//SxVm2OtpMlmM4zGuQ82N9YBaQy3nS2NjbYy08hnNI1rsPDikdQI0tBawhm91wsrgMsdXaRSfkEqy7ZpAOMH0ebMYck5r4l+OSXe7lR3XoA4CZU6Rd+eaQ9xHktcdtaKA=", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Cookie": 'PSTM=1646122173; BIDUPSID=4A46D96DC8423B1990661EC67CB17B97; __yjs_duid=1_5eebbceeddec85cf72e17b95994355071646122377960; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; APPGUIDE_10_0_2=1; BAIDUID=68FE11EE3BE9ADA190124B58EB36B628:FG=1; MCITY=-289%3A; H_PS_PSSID=; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1666148184,1666156674,1666168716,1666248882; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1666248882; BAIDUID_BFESS=68FE11EE3BE9ADA190124B58EB36B628:FG=1; delPer=0; PSINO=5; BA_HECTOR=218l21alahak2g00ak0k45e51hl268u1a; ZFY=tzTetLMt:AQKpcqHBdtdKVRVB8GNVB7XLc:BQRFb:BJvjQ:C; BDRCVFR[b3Q6T2w4uHD]=qdOo65HpBYTTh7znWc8mvqV; ab_sr=1.0.1_NWU3MmM0NWQyOWQxZmY5ZTBhZDNhODcwNTBmN2JkMjM3YmVjZWFlNzE2ODJmZWFjMzNhNWQwM2RmZDc4NGQ4ODJiMTVhMGUxZWM4YzkzMmI2ZDY2N2YzMWY0MWFkOWZkMjFkNzQ0YTc1NmFjOWJhNzRmZjVlZjUwZmNmNjc1ZTlhNzYxYTEzODkxY2NiZWMzOGVmMGIwMWFlYmNjNzhlYg==', "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36", } response = requests.post(url=url, data=Form_data, headers=headers, timeout=0.5) content = json.loads(response.content) # print(content) mean_list = [] if "result" in content: res = eval(content["result"].replace("null", "\"\""))["content"] for i in res[0]['mean']: # print(i) ptag = i["pre"] mean = list(i["cont"].keys()) # mean_list.append(ptag + ";".join(mean)) mean_list.extend(mean) # print("\n".join(mean_list)) if "data" in content: for i in content['data']: # print(i['dst']) mean_list.append(i['dst']) return mean_list def KM_en2ch(text): """ KM词典,通过html标签获取 https://kmcha.com :param text: :return: """ word = str(text).strip().replace(" ", "%20").replace("!", " ") # .replace(".", "") r = requests.get("https://kmcha.com/dict/{}".format(word.strip())) text = r.content.decode('utf8') # print(text) res_means = re.findall("
      (.+?)
    ", text, flags=re.S) if res_means: means = re.findall('])[^>]*?>(.+?)', res_means[0]) means = [i.replace("<", "<").replace(">", ">") for i in means if "人名" not in i] # print(means) # print(";##;".join(means)) return ";#;".join(means) return "" def Bing_en2ch(text): """ 必应词典,通过html标签获取 https://cn.bing.com/dict/ :param text: :return: """ print("raw_word:", text) word = str(text).replace("!", " ").replace(" ", " ").strip() while " " in word: word = word.replace(" ", " ") if len(re.findall("…+|\.{3,}", word)) > 1: word = re.sub("(…+|\.{3,})$", "", word.strip()) word = re.sub("(…+|\.{3,})\s*", "…", word.strip()) # word = word.replace(" ", "+").replace(".", "").strip() print(word) r = requests.get("https://cn.bing.com/dict/{}".format(word)) text = r.content.decode('utf8') if '
    ' not in text: r = requests.get("https://cn.bing.com/dict/search?q={}".format(word)) text = r.content.decode('utf8') # print(text) a = re.findall('
    (.+?)