查看2dj的游客区是否有新公告
前几天想抢一下2dj的激活码(失败),又不想一次又一次的刷新来看
所以写了个python的迷你脚本来看游客区的公告是否有新帖
用的是正则来匹配帖子,
再比较帖子的数目判断是否有新帖
代码如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
BTW,抢码失败,这个发码时间随意到令人发指。