zoukankan      html  css  js  c++  java
  • Python

    # -*- coding: utf-8 -*-
    
    # This code shows an example of text translation from English to Simplified-Chinese.
    # This code runs on Python 2.7.x and Python 3.x.
    # You may install `requests` to run this code: pip install requests
    # Please refer to `https://api.fanyi.baidu.com/doc/21` for complete api document
    
    import requests
    import random
    import json
    from hashlib import md5
    
    # Set your own appid/appkey.
    appid = 'INPUT_YOUR_APPID'
    appkey = 'INPUT_YOUR_APPKEY'
    
    # For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
    from_lang = 'en'
    to_lang =  'zh'
    
    endpoint = 'http://api.fanyi.baidu.com'
    path = '/api/trans/vip/translate'
    url = endpoint + path
    
    query = 'Hello World! This is 1st paragraph.
    This is 2nd paragraph.'
    
    # Generate salt and sign
    def make_md5(s, encoding='utf-8'):
        return md5(s.encode(encoding)).hexdigest()
    
    salt = random.randint(32768, 65536)
    sign = make_md5(appid + query + str(salt) + appkey)
    
    # Build request
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
    
    # Send request
    r = requests.post(url, params=payload, headers=headers)
    result = r.json()
    
    # Show response
    print(json.dumps(result, indent=4, ensure_ascii=False))

     http://api.fanyi.baidu.com/product/113

  • 相关阅读:
    【每日scrum】5.3
    Scrum仪式之Sprint计划会议
    软工结队开发--成员介绍
    java反射保存
    java后台开发传输乱码&&接口post传参失败
    润乾报表之分组
    润乾报表之居中无效(去空格)
    润乾报表之日期格式、小数位数
    润乾报表之序号、固定行数、统计
    润乾报表之条形码
  • 原文地址:https://www.cnblogs.com/watermarks/p/14790497.html
Copyright © 2011-2022 走看看