【Python】-001 获取CSDN博客访问数

【Python】-001 获取CSDN博客访问数

  最近在CSDN上写博客,看着访问数逐渐增加还是挺好玩的。但每次都需要点开来看并且没有访问数曲线记录,所以想着自己弄一个。本文主要是解决获取CSDN博客的访问数的问题。

1、工具及安装

  这个需求的本质是请求网页数据->获取返回结果->html文档解析->数据保存这些工作。
  其中主要涉及到的python包有urllib,beautifulsoup,time,re。其中各包的作用如下表所示

包名作用
urllib发起网页请求并接收服务器返回
beautifulsoup解析网页返回数据
time获取当前时间
re正则匹配

  beautifulsoup包需要自定义安装,从其官网上下载包(https://www.crummy.com/software/BeautifulSoup/),当前可下载的最新版本是4.6,下载包,解压。
这里写图片描述

图1 beautifulsoup目录结构

  使用python执行setup.py脚本,待执行完成后检查beautifulsoup是否安装成功。

2、代码

# -*- coding: UTF-8 -*-
from urllib import request
from bs4 import BeautifulSoup
import time
import re
if __name__ == "__main__":
    #1. Open the url by urllib
    response = request.urlopen("https://blog.csdn.net/freehawkzk")
    #2. Receive the response
    html = response.read()
    #3. Decode to fromat a human readable heml string
    html = html.decode("utf-8")
    #4. Parser the string 
    soup = BeautifulSoup(html,"html.parser")
    #5. According to view the source file of the aim page in firefox,
    #We have known that the aim node is named <dd> node, and the node
    #have a attribute named "title", the aim value is the value of <dd>
    #So firstly, we find all dd node
    dds = soup.find_all(re.compile("^dd"))
    for dd in dds:
        #Secondly, find the first node that have attributes
        if dd.attrs:
          #Get the value by key
          str1 = dd.attrs["title"]
          #Get current time
          timestr = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
          #6. Open file and save value
          with open(r'd:\blogVistorCount.txt', 'a+') as f:
            f.write(str1+" _ "+timestr + '\n')
          #7. Stop
          break

3、定时执行

  由于我是在windows10平台上进行的操作,所以采用的是windows的计划任务的方式来定时执行脚本。实现的效果是每天晚上20点执行请求并记录数据。
  
这里写图片描述
这里写图片描述
这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值