百度翻译API接口调用

#encodeing= utf-8
from datetime import datetime
import httplib
import md5
import urllib
import random
import json
import sys
reload(sys)
sys.setdefaultencoding('utf8')



def zh2en(CN_line):
    appid = ''
    secretKey = ''

    httpClient = None
    myurl = '/api/trans/vip/translate'
    q = CN_line
    fromLang = 'en'
    toLang = 'zh'
    salt = random.randint(32768, 65536)

    sign = appid + q + str(salt) + secretKey
    m1 = md5.new()
    m1.update(sign)
    sign = m1.hexdigest()
    myurl = myurl + '?appid=' + appid + '&q=' + urllib.quote(
        q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
        salt) + '&sign=' + sign

    try:
        httpClient = httplib.HTTPConnection('api.fanyi.baidu.com')
        httpClient.request('GET', myurl)


        response = httpClient.getresponse()
        result_line = response.read()
        # print(result_line)
        # print(type(result_line))
        return result_line
    except Exception as e:
        print(e)
    finally:
        if httpClient:
            httpClient.close()
    return 1


def get_dst(str_line):
    json_line = json.loads(str_line)
    new_result = json_line["trans_result"][0]["dst"]
    return new_result


#
def main(input_file,output_file):

    with open(input_file,'r') as rf:
        for x in range(25000):
            line = rf.readline().strip()
            try:
                line = line.split('\t')
                new_line =  line[1]
                num_line = line[0]
                str_line = zh2en(new_line)
            except Exception as e:
                print line+"????"
                continue
            try:
                line = get_dst(str_line)
            except Exception as e:
                print line+"!!!!!"
                continue
            with open(output_file,'a') as wr:
                try:
                    wr.write(num_line+'\t'+"["+line+"]")
                    wr.write('\n')
                except UnicodeEncodeError as e:
                    print line+"pppppp"
                    continue



if __name__ == '__main__':
    starttime = datetime.now()
    input_file = "xab"
    output_file = "corpus_cn2.txt"
    main(input_file,output_file)
    endtime = datetime.now()
    print "Translate Report Runtime %s s" % ((endtime - starttime).seconds)

你可能感兴趣的:(个人日记)