这一章节她讲的实操起来有点复杂,需要自己去建立账号等,所以干脆自己学习一下API
Application Program Interface,应用程序接口。
直接看一个实例(使用有道智云英文作文批改(文本输入)API 接口服务)看不懂课程的直接点下面的链接看有道保姆式教学👇:产品文档-作文批改https://ai.youdao.com/DOCSIRMA/html/%E4%BD%9C%E6%96%87%E6%89%B9%E6%94%B9/API%E6%96%87%E6%A1%A3/%E8%8B%B1%E8%AF%AD%E4%BD%9C%E6%96%87%E6%89%B9%E6%94%B9%EF%BC%88%E6%96%87%E6%9C%AC%E8%BE%93%E5%85%A5%EF%BC%89/%E8%8B%B1%E8%AF%AD%E4%BD%9C%E6%96%87%E6%89%B9%E6%94%B9%EF%BC%88%E6%96%87%E6%9C%AC%E8%BE%93%E5%85%A5%EF%BC%89-API%E6%96%87%E6%A1%A3.html以下是有道翻译英文作文批改(文本输入) API 在python上的操作实例:
# -*- coding: utf-8 -*-
import sys
import uuid
import requests
import hashlib
import time
import time
reload(sys)
sys.setdefaultencoding('utf-8')
YOUDAO_URL = 'https://openapi.youdao.com/correct_writing_text'
APP_KEY = '您的应用ID'
APP_SECRET = '您的应用密钥'
def encrypt(signStr):
hash_algorithm = hashlib.sha256()
hash_algorithm.update(signStr.encode('utf-8'))
return hash_algorithm.hexdigest()
def truncate(q):
if q is None:
return None
size = len(q)
return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]
def do_request(data):
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
return requests.post(YOUDAO_URL, data=data, headers=headers)
def connect():
q = "待输入的文字"
grade = "待输入年级"
data = {}
curtime = str(int(time.time()))
data['curtime'] = curtime
salt = str(uuid.uuid1())
signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
sign = encrypt(signStr)
data['appKey'] = APP_KEY
data['q'] = q
data['salt'] = salt
data['sign'] = sign
data['signType'] = "v3"
data['grade'] = grade
response = do_request(data)
contentType = response.headers['Content-Type']
if contentType == "audio/mp3":
millis = int(round(time.time() * 1000))
filePath = "合成的音频存储路径" + str(millis) + ".mp3"
fo = open(filePath, 'wb')
fo.write(response.content)
fo.close()
else:
print response.content
if __name__ == '__main__':
connect()
大师讲了又说不能学的零碎知识:
pwd是查看你当前所在目录的位置
(shi) huaweimatebook16@huaweiMateBook16deMacBook-Air ~ % pwd
/Users/huaweimatebook16
ls 列出当前目录下的文件
(shi) huaweimatebook16@huaweiMateBook16deMacBook-Air ~ % ls
Desktop(桌面) Downloads Movies Pictures orenge.py shi
Documents Library Music Public python入门 venv
pip list下载列表
(shi) huaweimatebook16@huaweiMateBook16deMacBook-Air ~ % pip list
Package Version
---------- -------
colorama 0.4.4
pip 21.3.1
setuptools 41.2.0
终端命令行进入python3 写一条执行一条 exit()
(shi) huaweimatebook16@huaweiMateBook16deMacBook-Air ~ % python3
Python 3.8.2 (default, Jun 8 2021, 11:59:35)
[Clang 12.0.5 (clang-1205.0.22.11)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 10
>>> print(a)
10
>>> exit()
cd( change directory)
(shi) huaweimatebook16@huaweiMateBook16deMacBook-Air ~ % cd .\Desktop\
>
创建
(shi) huaweimatebook16@huaweiMateBook16deMacBook-Air ~ % mkdir sb
删除
(shi) huaweimatebook16@huaweiMateBook16deMacBook-Air ~ % del .\sb\
>
下载
(shi) huaweimatebook16@huaweiMateBook16deMacBook-Air ~ % pip3 list
Package Version
---------- -------
colorama 0.4.4
pip 21.3.1
setuptools 41.2.0
终端里进入python3 help(print)
按Q退出