每日时政新闻前六,主要调用的是百度热搜的接口,网上已经有很多代码了,这边主要是结合企业微信使用,达到以下效果:
1.每日推送到企业微信应用,通过图文消息的方式;
2.卡片点进去就是热搜详情,非常适合考研小伙伴快速一扫每日的热点,不用被一些APP推送的垃圾新闻干扰,都是百度精选的新闻;
3.封面调用的是Bing的每日一图接口,每天都是美美的不重样;
代码如下
# -*- coding: utf8 -*-
from lxml import etree
import requests
import time
import json
import urllib.request
ID = "" ##企业ID
Secret = "" ##应用密码
UserID = "@all" ##成员ID列表(消息接收者,多个接收者用'|'分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
PartyID = 1 ##部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
AppID = 1000001 ##应用ID
def bingtu():
URL = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"
tu = requests.post(url=URL)
# print (tu.text)
json_response = tu.json()
try:
for item in json_response['images']:
picurl = "https://www.bing.com/"+item['url']
except:
picurl = "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
print("获取到今日美图:"+picurl)
return picurl
def resou():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36'}
page = requests.get("https://www.baidu.com", headers=headers)
html = page.text
element = etree.HTML(html)
ps = element.xpath('//*[@class="title-content-title"]')
alltxt = "\n"
if len(ps) > 0:
for p in ps:
text = p.text+"\n"
alltxt = alltxt+text
else:
alltxt="空"
print(alltxt)
return alltxt
def get_token():
gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(ID, Secret)
r = requests.get(gurl)
dict_result = (r.json())
print("获取到企业微信token:"+dict_result['access_token'])
return dict_result['access_token']
ef send_news(title, description, url, picurl):
msg = {"articles": [{"title": title,"description": description,"url": url,"picurl": picurl}]}
post_data = {}
post_data['touser'] = UserID
post_data['toparty'] = PartyID
post_data['msgtype'] = 'news'
post_data['agentid'] = AppID
post_data['news'] = msg
post_data['enable_id_trans'] = '0' # 表示是否是保密消息,0表示否,1表示是,默认0
post_data['enable_duplicate_check'] = '0'
post_data['duplicate_check_interval'] = '1800'
Gtoken = get_token()
purl3 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(Gtoken)
json_post_data = json.dumps(post_data, skipkeys=False, ensure_ascii=False)
request_post = urllib.request.urlopen(purl3, json_post_data.encode(encoding='UTF8'))
print("图文发送成功")
return request_post
def shijian():
ti = str(time.strftime('%Y-%m-%d')) # 时间
print(ti)
#hour = int(time.strftime("%H")) # 小时
#print(str(hour))
return ti
def gosend():
ti = shijian()#获取时间
ti = ti + "百度热搜"
pi = bingtu()#获取图片
ei = resou()#获取热搜
send_news(title=ti, description=ei, url="https://top.baidu.com/board?tab=realtime", picurl=pi)
def main():
gosend()
def main_handler(event, context):
return main()
if __name__ == '__main__':
main()
欢迎关注,欢迎大佬指出问题或者交流提供更优的方案~
---------