自制CSDN博客评论邮件提醒
生活随笔
收集整理的這篇文章主要介紹了
自制CSDN博客评论邮件提醒
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
CSDN 本身是有評論郵件提醒服務的。我知道,但是。。。。。。。。。。。
你們自己看吧。。。。
干啥啊?郵件轟炸機嘛??
拉黑,拉黑 !!!
自己搞一個了。
上代碼:
# -*- coding:utf-8 -*- import net import re import json from EmailClass import Email_Ldef 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 reviewCountprint pageReviewCountif 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:passdef getNewReviewDetail(homePage):try:# 獲取文章 idnewArticleIdResult = 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,postTimeexcept Exception as e:return Nonedef readReviewCount():try:with open(r'./review_count',"r") as f:reviewCount = f.read()except Exception as e:reviewCount = 0return reviewCountdef 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 &部署到我的樹莓派上,完成
總結
以上是生活随笔為你收集整理的自制CSDN博客评论邮件提醒的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用SSH 反向代理 ,实现跨局域网连接
- 下一篇: Linux awk+uniq+sort