requests获取当天知乎日报并推送到企业微信,主要是看“瞎扯·吐槽”

2023-04-26 20:31:40 浏览数 (1)

需要自己设置以下内容

代码语言:javascript复制
corpid = ''  #企业微信的 corpid
corpsecret = ''  #企业微信 corpsecret
appid = ''  #企业微信 appid
代码语言:javascript复制
import datetime
import json
import requests as req
import sys
 
stoday = datetime.datetime.now().strftime('%m%d')
full_stoday = datetime.datetime.now().strftime('%Y-%m-%d')
 
corpid = ''
corpsecret = ''
appid = ''
 
 
def send_wx(x):
    url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}'
    r = req.get(url, timeout=5)
    tokens = json.loads(r.text)['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": x
        },
        "safe": 0,
    }
    data = json.dumps(data)
    return req.post(url, data=data, timeout=9).text
 
 
headers = {
    'user-agent':
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}
r = req.get('https://news-at.zhihu.com/api/4/news/latest',
            headers=headers)  # 获取知乎API
rjson = json.loads(r.text)
if not rjson:
    sys.exit()
 
ls = [f'----- {full_stoday} -----']
for i in rjson['stories']:
    biaoti = i['title']
    url = i['url']
    riqi = i['ga_prefix'][:4]
 
    if riqi == stoday:
        if '·' in biaoti:
            biaoti = f'★{biaoti}'
        ls.append(f'n<a href="{url}">{biaoti}</a>')
 
send_wx('n'.join(ls))

0 人点赞