首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >使用python获取热搜榜单,每小时发送一次到钉钉群消息

使用python获取热搜榜单,每小时发送一次到钉钉群消息

原创
作者头像
JQ实验室
修改于 2022-03-21 17:02:53
修改于 2022-03-21 17:02:53
1.1K0
举报
文章被收录于专栏:实用技术实用技术

#! coding=utf-8

import requests

from bs4 import BeautifulSoup

import json

import schedule,time

def sendDing(msg):

'''

    发送钉钉消息功能

    '''

# 个人 

dingding_url = 'https://oapi.dingtalk.com/robot/send?access_token=token'

headers = {'Content-Type':'application/json;charset=UTF-8'}

send_data = json.dumps(msg).encode('utf-8')

ret = requests.post(url=dingding_url,data=send_data,headers=headers)

print(ret.text)

def getHot(url,from_msg):

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36'

    }

page_text = requests.get(url=url, headers=headers).text

#print(page_text)

soup_p = BeautifulSoup(page_text,'lxml')

tab = soup_p.find('table')

trs = tab.find_all('tr')

links = [{'title':'消息提醒:来自>'+from_msg,'messageURL':'aHR0cHM6Ly90b3BodWIudG9kYXk=','picURL':'https://staticedu-wps.cache.iciba.com/image/cae3e193caf289efe5d33bf63a37ad4b.jpg'}]

for tr in trs:

tds = tr.find_all('td')

#print(tds[0].text, tds[1].text,tds[2].text)

#print(tds[1].a.get('href'))

title = '{0} {1}({2})'.format(tds[0].text, tds[1].text,tds[2].text)

print(title)

messageURL = 'https://tophub.today' + tds[1].a.get('href')

item = {'title':title,'messageURL':messageURL,'picURL':'https://staticedu-wps.cache.iciba.com/image/cae3e193caf289efe5d33bf63a37ad4b.jpg'}

links.append(item)

msg = { "msgtype":"feedCard","feedCard":{'links':links}}

#dingding = json.dumps(msg)

sendDing(msg)

douyin_url = 'aHR0cHM6Ly90b3BodWIudG9kYXk=/n/K7GdaMgdQy'

weibo_url ='aHR0cHM6Ly90b3BodWIudG9kYXk=/n/KqndgxeLl9'

baidu_url='aHR0cHM6Ly90b3BodWIudG9kYXk=/n/Jb0vmloB1G'

schedule.every().hour.do(getHot,douyin_url,'抖音')

schedule.every().hour.do(getHot,weibo_url,'微博')

schedule.every().hour.do(getHot,baidu_url,'百度')

if __name__ == '__main__':

while True:

schedule.run_pending()

time.sleep(10)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档