用re找到微博热搜内容条目,
再转换成一个整体内容,
通过pushover提供的api接口发起推送,
当然手机上也要安装pushover。
就会收到消息推送了。
[Python] 纯文本查看 复制代码 import time
import requests
import re
url = 'https://s.weibo.com/top/summary?cate=realtimehot'
headers = {
'User-Agent': 'Chrome/96.0.4664.45 Safari/537.36','Cookie': 'SUB=_',
'Referer': 'https://s.weibo.com/'}
resp = requests.get(url,headers=headers)
page_content = resp.text.encode('gbk','ignore').decode('gbk') # 解决某些网站好多编码不能解析的问题
resp.close()
obj = re.compile(r'<td class="td-01 ranktop">(?P<rank>.*?)</td>.*?'
r'<a href="(?P<herf>.*?)" target="_blank">(?P<title>.*?)'
r'</a>.*?<span>(?P<hotness>.*?)</span>',re.S)
result = obj.finditer(page_content)
count = 0
weibo_list = []
for it in result:
if count < 20: # 控制推送条数
weibo_list.append(f"{it.group('rank')}.{it.group('title')}")
count += 1
push_url = 'https://api.pushover.net/1/messages.json'
# 在https://pushover.net 注册账户就会有user码,再创建个Application就有了token
# 手机也装个Pushover就能就收到消息了
message = str(weibo_list).replace('[','').replace(']','').replace("'",'').replace(',','\n')
data = {
"token":"xxxx", # 用自己的token
"user":"xxxx", # 用自己的user码
"title":f"微博热搜-{time.localtime()[1]}月{time.localtime()[2]}日", # 推送的标题
"message":message, # 推送的内容
"sound":"tugboat" # 推送的提示音类型
}
resp = requests.post(push_url,data=data)
print('推送成功' if resp.json()['status']==1 else '推送失败')
|