云学编程的20天—【微软官方python入门教程 P37笔记】2021-11-20 P37实操API —换成如何下载安装Python第三方库

 这一章节她讲的实操起来有点复杂,需要自己去建立账号等,所以干脆自己学习一下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退出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值