Python实时爬取CSDN博客阅读量增长图

基于requests 库与numpy 库 实现的实时博客增长监测系统。

原理

requests库
re正则表达式
numpy
条件判断
博客阅读突破%d
URL
HTML
matplotlib库
图像化显示
email库
邮件通知

效果预览

异常的断崖式增长、平直的增长线。

在这里插入图片描述

实现代码

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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值