python企业微信推送《每天60秒读懂世界》
本帖最后由 YANGLIGHT 于 2022-9-15 18:47 编辑之前看到这个 知乎每天60秒读懂世界 一直想着部署一个玩一玩
但是因为是个python初学者 且没空下来所以一直耽搁了
今天闲下心来写了个脚本
图片api来源: https://www.789dl.cn/zb.html
图片api:https://api.03c3.cn/zb
文字api接口:https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items?limit=1
推送效果:
# -*- coding = utf-8 -*-
# 若需要在短时间内大量推送消息,请自行更改代码,把获取的access_token保存起来使用
# access_token有效期目前是7200s,可以通过api返回信息查到有效时间
# 不要频繁调用api获取,会被腾讯拉黑的
import requests
def push_service(corpid, corpsecret, Agentid):
if corpid == "" or corpsecret == "" or Agentid == "":
print("必要参数为空")
return
# 获取access_token
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
r = requests.get(url).json()
if r["errcode"] != 0:
print(r["errmsg"])
print("access_token获取失败!")
return
else:
access_token = r["access_token"]
# 获取图片media_id
url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image"
img = requests.get('https://api.03c3.cn/zb').content
files = {'media': img}
response = requests.post(url=url, files=files).json()
media_id = response["media_id"]
# 推送
url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
"touser": "@all",
"msgtype": "image",
"agentid": Agentid,
"image": {
"media_id": media_id
}
}
msg = requests.post(url=url, json=data).json()
if msg["errcode"] != 0:
print(f"推送失败!\n{msg}")
else:
print('推送成功')
if __name__ == "__main__":
# 以下参数必填
corpid = "xxxxxxxxxxx"# 企业ID
corpsecret = "xxxxxxxxxxxxxxxxxxxxxxx"# Secret
Agentid = "xxxxx"# 应用ID
push_service(corpid, corpsecret, Agentid)
python 好学吗? 本帖最后由 wkdxz 于 2022-9-16 11:54 编辑
谢谢楼主分享的API,搞了个文字版,有兴趣的朋友拿走吧,需要套用企业微信API
import datetime
import re
import requests
stoday = datetime.datetime.now().strftime('%Y-%m-%d')
def send_wx(msg):
corpid = '***'
corpsecret = '***-**'
appid = '***'
url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}'
r = requests.get(url, timeout=5)
tokens = r.json()['access_token']
url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={tokens}"
data = {
"touser": "@all",
"msgtype": "text",
"agentid": appid,
"text": {
"content": msg
},
"safe": 0,
}
rtxt = requests.post(url, json=data, timeout=9).json()
return rtxt['errcode'] == 0 and rtxt['errmsg'] == 'ok'
def news_60s():
response = requests.get(
'https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items')
html = response.json()['data']['content']
cmd = r'data-pid="[^"]*">(\d+、[^;]*);</p>'
results = re.findall(cmd, html, re.S)
results.insert(0, f'{stoday} · 60秒新闻')
return '\n\n'.join(results).replace('"', '"')
if __name__ == '__main__':
news = news_60s()
print(news)
print(send_wx(news))
厉害厉害,学编程真是有用的技术 感谢分享,收藏学习 你这个接口不好用宇宙工具箱那个 感谢分享 牛逼的东东 学习了,学习使我快乐 不错 挺好玩 改天也搞一个 新闻也可以自己采集:lol