CSDN 本身是有评论邮件提醒服务的。我知道,但是。。。。。。。。。。。
你们自己看吧。。。。
干啥啊?邮件轰炸机嘛??
拉黑,拉黑 !!!
自己搞一个了。
上代码:
# -*- coding:utf-8 -*-
import net
import re
import json
from EmailClass import Email_L
def run():
try:
# 旧的评论数
reviewCount = readReviewCount()
# 获取网页上最新的评论数
homeUrl = "https://blog.csdn.net/qq_28766327"
homePage = net.getResponse(homeUrl).encode('utf8').decode('utf8') #注意这个编码问题
reviewCountResult = re.search(u'<dt>评论</dt>.*?"count">(\d*?)</span>',homePage,re.S|re.M)
if reviewCountResult != None:
pageReviewCount = reviewCountResult.group(1)
else:
pageReviewCount = reviewCount
# 如果网页上评论数比旧评论数多,证明有新评论
print reviewCount
print pageReviewCount
if str(reviewCount) != str(pageReviewCount):
# 将新评论数记录下来
writeReviewCount(pageReviewCount)
# 获取评论信息
content,userName,postTime = getNewReviewDetail(homePage)
message = "<b>"+userName+":</b>"+content+"<br>"+postTime
# 发送提醒邮件
emailObj = Email_L()
emailObj.senEmail(["1000000000@qq.com"],"【新博客评论】","html",message)
except Exception as e:
pass
def getNewReviewDetail(homePage):
try:
# 获取文章 id
newArticleIdResult = re.search(r'class="newcomment-list".*?https://blog.csdn.net/qq_28766327/article/details/(.*?)#comments">',homePage,re.S|re.M)
articleId = newArticleIdResult.group(1)
# 根据文章ID获取评论列表
reviewListUrl = "https://blog.csdn.net/qq_28766327/phoenix/comment/list/"+str(articleId)+"?page=1&size=15&tree_type=1"
reviewDataString = net.getResponse(reviewListUrl)
reviewData = json.loads(reviewDataString)
lists = reviewData["data"]["list"]
content = ""
userName = ""
postTime = ""
commentId = 0
# 找到最新一条评论
for item in lists:
info = item["info"]
if int(info["CommentId"]) > commentId:
commentId = info["CommentId"]
content = info["Content"]
userName = info["UserName"]
postTime = info["PostTime"]
if item.has_key("sub"):
subArray = item["sub"]
for sub in subArray:
if int(sub["CommentId"]) > commentId:
commentId = sub["CommentId"]
content = sub["Content"]
userName = sub["UserName"]
postTime = sub["PostTime"]
return content,userName,postTime
except Exception as e:
return None
def readReviewCount():
try:
with open(r'./review_count',"r") as f:
reviewCount = f.read()
except Exception as e:
reviewCount = 0
return reviewCount
def writeReviewCount(reviewCount):
with open(r'./review_count',"w") as f:
f.write(str(reviewCount))
if __name__ == "__main__":
run()
备注:
1. net 模块是我用 requets 封装的网络请求工具,功能是在子线程中进行网络请求(我做另一个项目的时候发现,几十万次网络请求可能有一次会卡死进程)。这里它不重要,不要管他
2. EmailClass 是我封装的发送邮件的工具,这里它也不重要。不要管他。如果不知道怎么发邮件,可以看我的其他文章,我专门写了一篇
然后就是定时监测了,很简单 利用 linux的 crontab 一个定时任务搞定
30 9-21 * * * cd /home/xxxxxx/xxxxx; python checkReview.py > checkReview.log 2>&1 &
部署到我的树莓派上,完成