这两天在论坛找了PDF,然后学习python3网络爬虫实战
下午的时候看到有人通过企业微信调用接口给自己发微信消息很有意思
自己试下
wework.py
[Python] 纯文本查看 复制代码 """
调用企业微信api,发送消息
"""
import requests
import json
req = requests.session()
tokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
workWxSendUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send"
def sendMessage(params):
data = packSendData(params)
token = token = {
'access_token': getToken(tokenUrl, params=params)
}
res2 = req.post(" https://qyapi.weixin.qq.com/cgi-bin/message/send",
params=token, data=json.dumps(data))
return res2.text
def getToken(url, params):
res = req.get(url, params=params)
jsonDict = res.json()
return jsonDict['access_token']
def packSendData(params):
data = {
"touser": "@all",
"msgtype": "text",
"agentid": params['agentid'],
"text": {
"content": params['description']
},
"safe": 0,
"enable_id_trans": 0,
"enable_duplicate_check": 0,
"duplicate_check_interval": 1800
}
return data
main.py
[Python] 纯文本查看 复制代码 import wework
data = {
'corpid':'',
'corpsecret':'',
'agentid':'',
'title':'领奖通知',
'description':'恭喜你中了1000W,领奖码:xxxx',
'url':''
}
res = wework.sendMessage(params=data)
print(res)
调用结果:
ps: python语法搜的菜鸟教程
爬虫,刚学完requests章节
|