基于Python自动发送QQ群消息
1.准备工作
此次测试基于python3,需要安装qqbot、bs4、requests库。
qqbot项目地址:https://github.com/pandolia/qqbot.git
pip qqbot
pip install bs4
pip install requests
#更改qqbot配置文件
以下配置是为了通过邮件发送授权二维码完成授权操作。
2.代码部分
#coding:UTF-8
import qqbot
import requests
import bs4
import time
##SentQQ
class QQ(object):
def __init__(self):
self.qqnumber = '1234567890'
self.message = '信息自动推送功能test!!!'
self.groupname = ['交流群']
self.bot = qqbot._bot
def sendMsgToGroup(self,msg,group,bot):
for group in group:
bg=bot.List('group', group)
if bg is not None:
bot.SendTo(bg[0],msg)
def main(self):
self.bot.Login(['-q', self.qqnumber])
self.sendMsgToGroup(self.message,self.groupname,self.bot)
##爬取数据
class GetData(object):
def __init__(self):
self.rooturl = 'https://www.xxxx.com'
self.url = self.rooturl + '/ah/20?lang=1'
self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}
def getdata(self):
response = requests.get(self.url,headers=self.headers)
if int(response.status_code) == 200:
htmldata = bs4.BeautifulSoup(response.text,'html.parser')
blognumber = 0
result = "[%s] 技术文章推荐: \n \n" %(time.strftime('%Y-%m-%d',time.localtime(time.time())))
for k in htmldata.find_all('a'):
if blognumber <= 10:
try:
if k['title'] != '':
blogurl = self.rooturl + str(k['href'])
blogtitle = str(k['title'])
bloginfo = "%d.[%s] \n %s \n" %(blognumber,blogtitle,blogurl)
result = result + bloginfo
blognumber += 1
except:
pass
return result
if __name__ == '__main__':
server = QQ()
server.message = GetData().getdata()
server.main()
3.添加计划任务
00 07 * * * python3 /root/bin/autoqq.py
4.效果图
关注公众号