基于requests 库与numpy 库 实现的实时博客增长监测系统。
原理
效果预览
异常的断崖式增长、平直的增长线。
实现代码
import requests
import re
import matplotlib.pyplot as plt
import numpy as np
import smtplib
from email.mime.text import MIMEText
import time
def getHTMLText(url):
'''爬取目标网页'''
headers = {}
r = requests.get(url,headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
#print(r.text)
return r.text
def paserPage(lst,html,count):
'''提取阅读量数字'''
catch = re.findall(r'访问量 \d*',html)
#print('catch =',catch)
num = eval(catch[0].split(' ')[1])
if count==1:
count = num
num -= count
else:
num -= count
lst.append(num)
return count
def Plot(y):
'''绘图'''
x = np.arange(len(y))
plt.plot(x,y)
plt.xlabel('Time/s'),plt.ylabel('Amount')
plt.pause(30)
plt.close()
def Email(num):
sender = '1650887506@qq.com'
receiver = '1650887506@qq.com'
subject = '博客订阅量'
smtpserver = 'smtp.qq.com'
username = '1650887506@qq.com'
password = 'htveoihjdjzighda'
msg = MIMEText('</pre><h1>博客阅读量突破%d</h1><pre>'%num,'html','utf-8')
msg['Subject'] = subject
smtp = smtplib.SMTP()
smtp.connect('smtp.qq.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
def main():
lst = []
url = 'https://blog.csdn.net/qq_43161186/article/details/104219604'
count = 1 #用于存储初始博客访问量
while True:
html = getHTMLText(url)
count = paserPage(lst,html,count)
print(lst)
Plot(lst)
if (lst[-1]+count)%1000==0:
Email(lst[-1]+count)
time.sleep(30)
if __name__ == "__main__":
main()