Python实现翻译脚本
1.调用谷歌翻译
import re import html from urllib import parse import requests GOOGLE_TRANSLATE_URL = 'http://translate.google.cn/m?q=%s&tl=%s&sl=%s' def translate(text, to_language="auto", text_language="auto"): text = parse.quote(text) url = GOOGLE_TRANSLATE_URL % (text,to_language,text_language) response = requests.get(url) data = response.text expr = r'(?s)class="(?:t0|result-container)">(.*?)<' result = re.findall(expr, data) if (len(result) == 0): return "" return html.unescape(result[0]) print(translate("你吃饭了么?", "en","zh-CN")) #汉语转英语 print(translate("你吃饭了么?", "ja","zh-CN")) #汉语转日语 print(translate("about your situation", "zh-CN","en")) #英语转汉语
2,爬虫类翻译
import hmac import base64 import time import uuid import requests class TextTranslate: def __init__(self, locale="zh-CN", source="zh-CN", target="ko"): """ 初始化一个翻译对象 :param locale: 所在地区 :param source: 原文字类型 en 英文 ko 韩文 zh-CN 中文 :param target: 要翻译成什么文字 en 英文 ko 韩文 zh-CN 中文 """ self.locale = locale self.source = source self.target = target self.url = "https://papago.naver.com/apis/n2mt/translate" self.key = "v1.6.4_4b23b99383" @property def __device_id(self): device_id = str(uuid.uuid4()) return device_id @property def __timestamp(self): timestamp = str(int(time.time() * 1000)) return timestamp def __has(self, device_id, timestamp): hmacs = device_id + "\n" + self.url.split("?")[0] + "\n" + timestamp hmac_obj = hmac.new(self.key.encode("utf8")) hmac_obj.update(hmacs.encode("utf8")) hmacb = hmac_obj.digest() md5b64 = base64.b64encode(hmacb).decode("utf8") return md5b64 def __get_headers(self, timestamp, device_id): headers = { "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36", "timestamp": timestamp, "authorization": "PPG " + device_id + ":" + self.__has(device_id, timestamp), } return headers def __get_data(self, device_id, text): data = { "deviceId": device_id, "locale": self.locale, "dict": False, "dictDisplay": 30, "honorific": False, "instant": True, "paging": False, "source": self.source, "target": self.target, "text": text } return data def translate(self, text): device_id = self.__device_id timestamp = self.__timestamp headers = self.__get_headers(timestamp, device_id) data = self.__get_data(device_id, text) res = requests.post(url=self.url, headers=headers, data=data) return res.json() if __name__ == '__main__': translate = TextTranslate(source="en", target="zh-CN") print(translate.translate("hello")["translatedText"])
3.Python调用有道翻译的脚本
import urllib.request import urllib.parse import json def get_data(words): data = {} data["type"] = "AUTO" data["i"] = words data["doctype"] = "json" data["xmlVersion"] = "1.8" data["keyfrom:fanyi"] = "web" data["ue"] = "UTF-8" data["action"] = "FY_BY_CLICKBUTTON" data["typoResult"] = "true" data = urllib.parse.urlencode(data).encode('utf-8') return data def url_open(url, data): req = urllib.request.Request(url, data) req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36") response = urllib.request.urlopen(req) html = response.read() html = html.decode("utf-8") return html def get_json_data(html): result = json.loads(html) result = result['translateResult'] result = result[0][0]['tgt'] return result def main(): words = input("please input words: ") url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=dict.top" data = get_data(words) html = url_open(url, data) result = get_json_data(html) print("The result: %s" % result) if __name__ == "__main__": while True: main()
每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)