python 百度翻译官方api和破解版方法

百度翻译开放平台,收费标准: 
若当月翻译字符数≤2百万,当月免费;若超过2百万字符,按照49元/百万字符支付当月全部翻译字符数费用

官方python版调用代码

# -*- coding: utf-8 -*- 
# 百度翻译平台官方 python版代码, 使用python 2.7 

import httplib
import md5
import urllib
import random
import json
import datetime 
import sys

print(sys.getdefaultencoding())
reload(sys)
sys.setdefaultencoding('utf-8')
print(sys.getdefaultencoding())

appid = 'yourid_yourid' #你的appid
secretKey = 'yoursecret_yoursecret' #你的密钥
myurl = '/api/trans/vip/translate'

def trans_baidu(fromlan,tolan,text):
    httpClient = None
    q = text.encode("utf-8")
    fromLang = fromlan
    toLang = tolan
    salt = random.randint(32768, 65536)

    sign = appid+q+str(salt)+secretKey
    m1 = md5.new()
    m1.update(sign)
    sign = m1.hexdigest()
    the_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', the_myurl)
    
        #response是HTTPResponse对象
        response = httpClient.getresponse()
        res=response.read()
        print res
        json_data=json.loads(res)
        ret = json_data["trans_result"][0]["dst"]
        #print ret
        return ret
    except Exception, e:
        print e
        return str(e)
    finally:
        if httpClient:
            httpClient.close()

主要将 其中appid 和 secretKeyf赋值为申请百度翻译开放平台中的APP ID 和秘钥。

官方代码是python2.7。 将其转换为python3

以下为 换换过程中的 python2.7和python3的区别:

httplib在python3中替换为了 http.client

# python 2.7
import httplib
httplib.HTTPConnection("api.fanyi.baidu.com")
# python3
import http.client
http.client.HTTPConnection("api.fanyi.baidu.com")

md5在python3中替换为 hashlib.md5

# python 2.7
import md5
m1 = md5.new()
m1.update(stringstring)
# python 3
import hashlib
sign=hashlib.md5(sign.encode()).hexdigest()

urllib在python3中的不同

# python 2.7
import urllib
urlib.quote(query_text)
# python 3
import urllib
urllib.parse.quote(query_text)

Python3 完整代码: 

import http.client
import hashlib
import urllib
import random
import json
import datetime 
import sys

appid = 'yourid_yourid' #你的appid
secretKey = 'yoursecret_yoursecret' #你的密钥
myurl = '/api/trans/vip/translate'

def trans_baidu(fromlan,tolan,text):
    httpClient = None
    q = text
    fromLang = fromlan
    toLang = tolan
    salt = random.randint(32768, 65536)

    sign = appid+q+str(salt)+secretKey
    sign = hashlib.md5(sign.encode()).hexdigest()
    the_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')
        httpClient.request('GET', the_myurl)
    
        #response是HTTPResponse对象
        response = httpClient.getresponse()
        res=response.read()
        print (res)
        json_data=json.loads(res)
        ret = json_data["trans_result"][0]["dst"]
        #print ret
        return ret
    except Exception as e:
        print (e)
        return str(e)
    finally:
        if httpClient:
            httpClient.close()

篇幅有点长,破解版的源码另起一篇: https://blog.csdn.net/enter89/article/details/88289785

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值