【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点执行请求并记录数据。
  
这里写图片描述
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python-gitlab是一个用于与GitLab API进行交互的Python库。它提供了一组API方法,可以用于管理GitLab仓库、项目、用户等各种资源。你可以使用Python-gitlab来创建、删除、更新仓库,管理用户权限,获取仓库的分支和标签等操作。 要安装Python-gitlab,你可以按照以下步骤进行操作: 1. 添加GitLab的Yum仓库,可以使用以下命令将GitLab的Yum仓库添加到你的系统中: ```bash sudo curl -L https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash ``` 2. 安装Python-gitlab,运行以下命令: ```bash sudo yum install python-gitlab ``` 3. 配置GitLab的API访问令牌,你可以按照引用中提到的方法,在GitLab的设置页面生成一个访问令牌。这个令牌将用于认证你的Python-gitlab应用程序与GitLab API的交互。 安装完成后,你就可以使用Python-gitlab库来与GitLab API进行交互了。可以使用以下常用命令进行操作: - `gitlab.projects.create()`:创建一个新的GitLab项目。 - `gitlab.projects.delete()`:删除一个GitLab项目。 - `gitlab.projects.get()`:获取一个GitLab项目的详细信息。 - `gitlab.projects.list()`:列出所有GitLab项目。 - `gitlab.users.create()`:创建一个新的GitLab用户。 - `gitlab.users.delete()`:删除一个GitLab用户。 - `gitlab.users.get()`:获取一个GitLab用户的详细信息。 - `gitlab.users.list()`:列出所有GitLab用户。 你可以根据你的具体需求,使用这些命令来管理你的GitLab仓库和用户。请根据需要参考引用中提到的更多常用命令,以及Python-gitlab的官方文档来了解更多功能和用法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python-gitlab管理gitlab以及gitlab基本安装配置使用](https://blog.csdn.net/u014118698/article/details/120197140)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值