美文网首页
百度翻译API的python调用

百度翻译API的python调用

作者: Leo_23 | 来源:发表于2018-08-03 21:50 被阅读14次

    百度翻译API的python调用

    Gist

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    __author__ = "leo"
    __time__ = "2018-08-03"
    
    import requests
    import json
    import hashlib
    
    
    def md5_sign(s):
        return hashlib.md5(s.encode('utf8')).hexdigest()
    
    
    def baidu_fanyi(q):
        url = "http://api.fanyi.baidu.com/api/trans/vip/translate"
        f = 'en'
        to = 'zh'
        appid = 'xxx'   # 自己申请
        salt = '1435660288'
        key = 'xxx'  # 自己申请
        finial_str = '%s%s%s%s' % (appid, q, salt, key)
        sign = md5_sign(finial_str)
        params = {
            'q': q,
            'from': f,
            'to': to,
            'appid': appid,
            'salt': salt,
            'sign': sign,
    
        }
        res_json = requests.get(url, params=params).json()
        res = res_json['trans_result'][0]["dst"]
        print(res)
    
    
    if __name__ == '__main__':
        baidu_fanyi('apple')
    
    

    相关文章

      网友评论

          本文标题:百度翻译API的python调用

          本文链接:https://www.haomeiwen.com/subject/dcauvftx.html