File size: 1,029 Bytes
5a79bd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import requests
import os
from .utils.AuthV3Util import addAuthParams

# 您的应用ID
APP_KEY = os.environ.get("APP_KEY",None)
# 您的应用密钥

APP_SECRET = os.environ.get("APP_SECRET",None)


def createRequest(msq):
    '''
    note: 将下列变量替换为需要请求的参数
    '''
    q = msq
    lang_from = 'auto'
    lang_to = 'en'
    # vocab_id = '您的用户词表ID'

    data = {'q': q, 'from': lang_from, 'to': lang_to}

    addAuthParams(APP_KEY, APP_SECRET, data)

    header = {'Content-Type': 'application/x-www-form-urlencoded'}
    res = doCall('https://openapi.youdao.com/api', header, data, 'post')
    print(str(res.content, 'utf-8'))
    return str(res.content, 'utf-8')


def doCall(url, header, params, method):
    if 'get' == method:
        return requests.get(url, params)
    elif 'post' == method:
        return requests.post(url, params, header)

# 网易有道智云翻译服务api调用demo
# api接口: https://openapi.youdao.com/api
if __name__ == '__main__':
    createRequest()