电子邮件编程、json、API、天气、钉钉机器人

2021-07-27  本文已影响0人  秋天丢了李姑娘

昨天回顾

paramiko 模块

此模块实现了 ssh 客户端的功能

ssh 命令 可以远程登录一台主机并远程操作这台主机

pip3 install paramiko

python 的电子邮件编程

实验手册见: http://tedu.weimingze.com/static/py_library/python_email.html

json

json 全称( Javascript Object Notation) , 是一种轻量级的数据交互格式

json 是基于 Javascript 编程语言的格式

JSON 采用完全独立于编程语言的文本格式

标准JSON的合法符号:

Json 的数据类型

字典中的两个人

[
  {
    'name': 'weimingze',
    'age': 35
  },
  {
    'name': '小王',
    'age': 18,
    'score': [90, 100, 85]
  },
]

JSON中的两个人

[
  {
    "name": "weimingze",
    "age": 35
  },
  {
    "name": "小王",
    "age": 18,
    "score": [90, 100, 85]
  },
]
Python Json
dict {}object(对象)
list, tuple []array数组
str "" string 字符串
int, float number 数字
True/False true/false
None null

import json

json模块的四个常用函数

函数 说明
json.dump(obj, fw) 把python 对象编码为json 字符串并写入文件fw中
json.dumps(obj) 把python 对象编码为json 字符串并返回
json.load(fr) 从文件流对象fr中读取json数据解码后返回python对象
json.loads(json) 从json 字符串中解码Python 对象

示例见:

>>> infos = [
  {
   'name': 'weimingze',
   'age': 35,
   'gender': True,
   'score': None
  },
  {
   'name': '小王',
   'age': 18,
   'score': [90, 100, 85]
  },
]
>>> infos
[{'name': 'weimingze', 'age': 35, 'gender': True, 'score': None}, {'name': '小王', 'age': 18, 'score': [90, 100, 85]}]
>>> 
>>> import json
>>> s = json.dumps(infos)  # 将infos 绑定的对象转为json 的字符串
>>> s
'[{"name": "weimingze", "age": 35, "gender": true, "score": null}, {"name": "\\u5c0f\\u738b", "age": 18, "score": [90, 100, 85]}]'
>>> objs = json.loads(s)  # 将 json 的字符串,转为 python 的对象
>>> objs
[{'name': 'weimingze', 'age': 35, 'gender': True, 'score': None}, {'name': '小王', 'age': 18, 'score': [90, 100, 85]}]
>>> type(objs)
<class 'list'>

API

Application Programming Interface 应用程序接口。在Web中,API 通常指HTTP协议的接口

requests 模块

import requests

url = 'https://www.sogou.com/web'
# https://www.sogou.com/web?query=linux
s = input('请输入查询的内容:')
params = {'query': s}
headers = {
 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
}   # 带上 'User-Agent' 请求头,把自己伪装成浏览者

r = requests.get(url, params=params, headers=headers)  # 发出get请求,传入 ?query=s 查询字符串
 # 相当于请求:https://www.sogou.com/web?query=linux
with open('sogou_' + s + '.html', 'wb') as fw:
 fw.write(r.content)   # 把 r.content 字节串写入文件

python 实验

自己实现

阿里云开发者平台

网址: www.aliyun.com

实验手册参见: http://tedu.weimingze.com/static/py_library/python_ali_api.html

AppCode:

e3e8438c45584a768f11eef0a99f2b9a

import requests

# API 的地址
url = 'http://jisuqgtq.market.alicloudapi.com/weather/query'

# 请求头
headers = {
    # 根据API的要求,定义相对应的Content - Type
    'Content-Type': 'application/json; charset=UTF-8',
    # 权限指定 APPCODE
    'Authorization': 'APPCODE e3e8438c45584a768f11eef0a99f2b9a'
}

# 设置查询参数
params = {'citycode': '101010100'}  # 城市代码

r = requests.get(url, headers=headers, params=params)
# print('r.json=', r.text)  # json 数据
# r 即为相应对象其中包括天气信息
data = r.json()  # data 绑定字典
# print(data)
# 使用pprint 模块进行打印
import pprint
# pprint.pprint(data)

print('今晚的最低温度:')
pprint.pprint(data['result']['daily'][0]['night']['templow'])

使用钉钉机器人

实验手册:http://tedu.weimingze.com/static/py_library/python_dingtalk.html

webhook API

https://oapi.dingtalk.com/robot/send?access_token=5c55ff3b8f338041a4b04e82c13c2fea3ff3edf3844fde2d99261607aae54a57

上一篇 下一篇

猜你喜欢

热点阅读