Python抓取我的CSDN粉丝数,白嫖GithubAction自动抓取

《Python抓取我的CSDN粉丝数,白嫖GithubAction自动抓取》

一.介绍

这段时间我想申请CSDN的博客专家认证,但是我发现我的总访问量不够(博客专家的总访问量要大于20万),所以我就想把我的CSDN每天的 【总访问量】,【原创】,【排名】,【粉丝】,【铁粉】这几个数据记录下来。
在这里插入图片描述
这里使用PythonGithubAction来实现我上面的需求。

二.实现

1.python爬取csdn主页数据

【这里我使用的IDE是PyCharm】
新建csdn_crawler.py文件用来抓取我的CSDN主页,具体代码如下:

import datetime
import requests
import bs4

if __name__ == '__main__':
    url = 'https://blog.csdn.net/qq_34035956'
    headers = {
        'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36(KHTHL, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
    }
    html_file = requests.get(url, headers=headers)
    obj_soup = bs4.BeautifulSoup(html_file.text, 'lxml')

    result = []
    names = obj_soup.select('div .user-profile-statistics-name')
    numbers = obj_soup.select('div .user-profile-statistics-num')
    for i in range(len(numbers)):
      result.append("{}: {}".format(names[i].text, numbers[i].text))

    now_time = datetime.datetime.now()
    year = now_time.year
    month = now_time.month
    day = now_time.day
    hour = now_time.hour
    minute = now_time.minute
    second = now_time.second

    output = "\n{}_{}_{}_{}_{}_{}\t {}".format(year, month, day, hour, minute, second, result)
    with open("./csdn_data.txt", mode="a") as f:
      f.write(output)

运行后可以看到自动生成了csdn_data.txt文件,并且有以下内容:
在这里插入图片描述
可以看到,我们要的几个数据【[‘总访问量: 123,062’, ‘原创: 179’, ‘排名: 7,858’, ‘粉丝: 1,524’, ‘铁粉: 131’]】都在这里面了。

2.配置GithubAction

简单列一下步骤

  1. 新建Github仓库
  2. 新建.github目录,新建.github/workflow目录
  3. 在.github/workflow目录下新建craw.yml

craw.yml用来配置GitHub Action自动化工作流,内容如下:

name: 定时抓取

on:
  workflow_dispatch:
  schedule:
    - cron: '0 * * * *'   #Runs every hour, on the hour
    #- cron: '0 23 * * *'  #Runs at 23:00 UTC every day.

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: run start.sh
        run: |
          bash ./start.sh
          
      - name: GitHub Push
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: $\{{ secrets.GITHUB_TOKEN }}
          branch: main

3.新建start.sh调用csdn_crawler.py

第2点的craw.yml中,我们调用了start.sh这个文件,现在我们就在仓库最外层新建start.sh这个文件,文件内容如下:

pip install beautifulsoup4
pip install lxml
python3 ./csdn_crawler.py

year=`date +%Y `
month=`date +%m `
day=`date +%d `
hour=`date +%H`
min=`date +%M`
now=$year-$month-$day-$hour-$min

git config --global user.email "13538898378@163.com"
git config --global user.name "hankangwen"

git add .
git commit -m "$now"

把上面git config的email和name改为你自己的名字。

4.启用Github Action

在Actions下启用一个Workflows,可以看到我这里已经有一个名字为“定时抓取”的workflow了。
在这里插入图片描述

三.开源地址

上面所有的代码都在下方这个开源仓库里,如果你没有成功配置可以fork我的仓库,改一下抓取的url地址,然后启用一下Github Action就可以了。
开源地址:https://github.com/hankangwen/MyCSDNData
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kerven_HKW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值