使用python进行翻译

百度翻译api

Posted by Kingtous on July 6, 2019

要用到的包

  • Urllib 网络请求
  • Json 解析json数据
  • Hashlib 构造md5加密值
  • Re 正则表达式

代码示例

一个用在汉化项目的代码

  • 通过正则表达式匹配相应要翻译的内容
  • 传入百度翻译api进行翻译
  • 得到翻译结果,构造语句,并写入磁盘
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import urllib.request
import urllib.parse
import json
import hashlib
import time, re

encoding = 'utf-8' # 编码用utf-8
salt = '666' #随机数
appid = '百度申请的appid'
secret_key = '百度申请的密钥'
# 请求失败码
REQUEST_FAILED = -1

# 正则匹配
settings_regex = r"\s*\'.+\'\s*=>\s*.+"


def getMD5(content):
    m2 = hashlib.md5()
    m2.update(content.encode(encoding))
    return m2.hexdigest()


def getTranslateResponce(url, data):
    data = urllib.parse.urlencode(data).encode('utf-8')
    response = urllib.request.urlopen(url, data)
    return response.read().decode('utf-8')


def trans(content):
    url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
    data = {}
    data['appid'] = appid
    data['salt'] = salt
    data['from'] = 'en'
    data['to'] = 'zh'
    data['q'] = content
    data['sign'] = getMD5(appid + content + salt + secret_key)
    html = getTranslateResponce(url, data)
    target = json.loads(html)
    while target.get('error_code', REQUEST_FAILED) != REQUEST_FAILED:
        # print('本次请求失败,原因为:',target['error_msg'])
        time.sleep(1)
        html = getTranslateResponce(url, data)
        target = json.loads(html)
    # print(target)
    return target['trans_result'][0]['dst']


def translate():
    file = open('en.php', 'r')
    output = open('cn.php', 'w')
    for line in file.readlines():
        # print(line)
        if re.match(settings_regex, line):
            result = re.search(r"\'[^=]+\'", line)
            # original_text 待翻译文本
            original_text = result.group()
            translated_text = trans(original_text).lower()
            translated_text = translated_text.replace('”', '')
            translated_text = translated_text.replace('“', '')
            if translated_text[0] != "'":
                translated_text = "'" + translated_text
            if translated_text[-1] != "'":
                translated_text = translated_text + "'"
            # print(translated_text)
            # print(original_text,' => ',translated_text)
            line = "    " + original_text + " => " + translated_text + ",\n"
            line = line.replace('"', "'")
        output.write(line)
        output.flush()
        print("写入:" + line, end='')
    file.close()
    output.close()


if __name__ == '__main__':
    # translate()
    text = trans('dick')
    print(text)