python自动截图发到企业微信

2023-06-23 16:24 综合百科 0阅读 投稿:小七

要实现自动发送每日简报到企业微信群,可以使用以下步骤:

首先需要在企业微信管理后台创建一个机器人(也叫Webhook),并获取相应的Key,用于后续发送消息。具体操作可以参考官方文档。

编写Python脚本,获取当日需要发送的消息内容,并使用企业微信机器人发送到指定的群组。具体步骤如下:

解析获取到的HTML内容,提取需要发送的消息内容;

实例化一个企业微信机器人对象,使用该对象的send_message()方法发送消息到指定的企业微信群组。

使用定时任务工具,如schedule模块,定期执行上述Python脚本,从而实现每日自动发送简报到群组的功能。

以下是一份示例代码,可以供你参考:

import requestsimport reimport scheduleimport timeclass WeChatBot: def __init__(self, key): self.wxurl = f'***/cgi-bin/webhook/send?key={key}' def send_message(self, cOntent='', mentioned_list=[], mentioned_mobile_list=[]): data = { "msgtype": "text", "text": { "content": content, "mentioned_list": mentioned_list, "mentioned_mobile_list": mentioned_mobile_list } } res = requests.post(self.wxurl, json=data).json()def get_daily_brief(): # 请求当日需要发送的消息内容,这里以网页爬虫获取新闻内容为例 url = "***.cn/china/" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36' } rsp = requests.get(url, headers=headers) cOntent= rsp.content.decode('utf-8', 'ignore') news_list = re.findall('(?<=<a href=).*?(?=>).*?(?=</a>)', content) msg = '' for i, news in enumerate(news_list): title = re.findall('(?<=>).*', news)[0] link = re.findall('(?<=href=").*(?=")', news)[0] msg += f'{i + 1}. <a href="{link}">{title}</a>\n' return msgif __name__ == "__main__": # 填写你自己的企业微信机器人Webhook Key bot = WeChatBot('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx') # 定时执行发送简报任务,每天早上7点半发送 schedule.every().day.at('07:30').do( lambda: bot.send_message(cOntent=get_daily_brief(), mentioned_list=['@all'])) while True: schedule.run_pending() time.sleep(1)

此代码将每天早上7点半发送当日新闻到企业微信群组,你可以根据自己的需求调整发送时间和消息内容。需要注意的是,在实际使用中,你需要将Webhook Key替换成自己的Key,才能使代码正常运行。

python自动截图发到企业微信图1
声明:若水百科所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系youzivr@vip.qq.com