此次测试基于python3,需要安装qqbot、bs4、requests库。
qqbot项目地址:https://github.com/pandolia/qqbot.git
pip install qqbot
pip install bs4
pip install requests
image.png
代码:
<span style="font-size: 16px;">#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()
</span>
image.png
python 路径代码的路径 查看是否授权成功>我没有成功。。
网友评论