查看2dj的游客区是否有新公告

Posted acodingdg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查看2dj的游客区是否有新公告相关的知识,希望对你有一定的参考价值。

前几天想抢一下2dj的激活码(失败),又不想一次又一次的刷新来看

所以写了个python的迷你脚本来看游客区的公告是否有新帖

用的是正则来匹配帖子,

再比较帖子的数目判断是否有新帖

 

代码如下

 

技术分享图片
import re
from bs4 import BeautifulSoup
from urllib import request
from time import sleep
from tkinter import messagebox

def msgBox():
    warn = messagebox.showinfo("Hello", "Hello")
    print(warn)

url = "http://bbs4.2djgame.net/home/forum.php?mod=forumdisplay&fid=360&filter=typeid&typeid=911"
headers = {"User-Agent":"User-Agent:Mozilla/5.0"}
url_with_headers = request.Request(url, headers=headers)
html_doc = request.urlopen(url_with_headers)
soup = BeautifulSoup(html_doc, html.parser)
target_id = re.compile(r"normalthread_[0-9]+")
tbodys = soup.find_all(tbody, id=target_id)
pre = len(tbodys)

while True:
    url = "http://bbs4.2djgame.net/home/forum.php?mod=forumdisplay&fid=360&filter=typeid&typeid=911"
    headers = {"User-Agent":"User-Agent:Mozilla/5.0"}
    url_with_headers = request.Request(url, headers=headers)
    html_doc = request.urlopen(url_with_headers)
    soup = BeautifulSoup(html_doc, html.parser)
    target_id = re.compile(r"normalthread_[0-9]+")
    tbodys = soup.find_all(tbody, id=target_id)
    cur = len(tbodys)
    if cur == pre:
        print("No New")
    else:
        print("New One")
        msgBox()
        break
View Code

BTW,抢码失败,这个发码时间随意到令人发指。

以上是关于查看2dj的游客区是否有新公告的主要内容,如果未能解决你的问题,请参考以下文章