使用python调用百度翻译api进行翻译

要用到的包

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

代码示例

一个用在汉化项目的代码

  • 通过正则表达式匹配相应要翻译的内容
  • 传入百度翻译api进行翻译
    • api说明
  • 得到翻译结果,构造语句,并写入磁盘
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('huawei')
    print(text)

你可能感兴趣的:(学习)