^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

实现网站消息提醒功能,适合所有网站和app

因为小雨在吾爱破解的会员等级不够,无法实时接收到新消息提醒,所以发布了一个 python 定时通知的方式,也首发在吾爱破解论坛上,本文只是以吾爱破解论坛消息提醒功能为例子来编写代码

这个消息提醒功能,原则上这种办法适合所有网站的消息提醒功能,大家可以在这个基础上进行发散开发(学习一定要学会举一反三,不然就是个榆木脑袋了)

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

# -*- coding: utf8 -*-
import json
import requests
import bs4
import time
token = 'your token'#在发送消息页面可以找到
 
def pushinfo(text,desp):
    headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
    'ContentType': 'text/html'
    }
    notifyurl = 'https://api.ossec.cn/v1/send?token=%s&topic=%s&message=%s'%(token,text,desp)
    requests.get(notifyurl,headers=headers)
 
  
def main_handler(event, context):
    headers={
        'Cookie': 'your cookie',
        'ContentType':'text/html;charset=gbk',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
    }
    html=requests.get('https://www.52pojie.cn',headers=headers)
    bsObj =bs4.BeautifulSoup(html.content,'html.parser')
    parent=bsObj.find('ul',id='myprompt_menu')
    num = len(parent.find_all('li'))
    if num > 3:
        text =  u'{0} 吾爱破解有新消息回复'.format(time.strftime("%Y-%m-%d %H:%M", time.localtime()))
        msgNum = parent.find('span',class_='rq').get_text()
        pushinfo(text, "消息数:" + msgNum + "条")
    # else:
    #   pushinfo("暂无消息",0)

使用说明:
1、腾讯云云函数设置定时任务,比如 20 分钟或者 1 小时 或者一天,频率自己设置,。鉴于本版块有很多布置云函数的帖子,我就不赘述了。
2、通知选择的是欧赛信令,相比于 server 酱来说,目前可以显示正文详情,可能是用的人少吧,反正能满足我的日常使用,大家可以自由选择。
3、代码中的 token 和 cookie 需要更改为自己的

代码功能原理:登录后判断网站消息提醒处对应 div 的子元素个数,当然了这个是通过对比后发现的。

没有消息时的 html 结构

<ul id="myprompt_menu" class="p_pop" style="position: absolute; z-index: 301; left: 1305.96px; top: 72.6px;" disautofocus="true" initialized="true">
<li><a href="home.php?mod=space&do=pm" id="pm_ntc" style="background-repeat: no-repeat; background-position: 0 50%;"><em class="prompt_news_0"></em>消息</a></li>
<li><a href="home.php?mod=follow&do=follower"><em class="prompt_follower_0"></em>新听众</a></li>
<li class="ignore_noticeli"><a href="javascript:;" onclick="setcookie('ignore_notice', 1);hideMenu('myprompt_menu')" title="暂不提醒"><em class="ignore_notice"></em></a></li>
</ul>

有消息时候时的 html 结构

<ul id="myprompt_menu" class="p_pop" style="position: absolute; z-index: 301; left: 1268.88px; top: 72.6px;" disautofocus="true" initialized="true">
<li><a href="home.php?mod=space&do=pm" id="pm_ntc" style="background-repeat: no-repeat; background-position: 0 50%;"><em class="prompt_news_0"></em>消息</a></li>
<li><a href="home.php?mod=follow&do=follower"><em class="prompt_follower_0"></em>新听众</a></li>
<li><a href="home.php?mod=space&do=notice&view=system"><em class="notice_system"></em>系统提醒(<span class="rq">1</span>)</a></li>
<li class="ignore_noticeli"><a href="javascript:;" onclick="setcookie('ignore_notice', 1);hideMenu('myprompt_menu')" title="暂不提醒"><em class="ignore_notice"></em></a></li>
</ul>

效果图:

实现网站消息提醒功能,适合所有网站和app

赠人玫瑰,手有余香。