基于Python實(shí)現(xiàn)文章信息統(tǒng)計(jì)的小工具
前言
博客園在個(gè)人首頁有一個(gè)簡(jiǎn)單的博客數(shù)據(jù)統(tǒng)計(jì),以博客園官方的首頁為例:

但是這些數(shù)據(jù)不足以分析更為細(xì)節(jié)的東西
起初我是想把博客園作為個(gè)人學(xué)習(xí)的云筆記,但在一點(diǎn)點(diǎn)的記錄中,我逐漸把博客園視為知識(shí)創(chuàng)作和知識(shí)分享的平臺(tái)
所以從年后開始,就想著做一個(gè)類似 CSDN 里統(tǒng)計(jì)文章數(shù)據(jù)的工具

這樣的統(tǒng)計(jì)功能可以更好的去分析讀者對(duì)于內(nèi)容的需求,了解文章內(nèi)容的價(jià)值,以及從側(cè)面認(rèn)識(shí)自己在知識(shí)創(chuàng)作方面的能力
說了不少無關(guān)的話,下面直接進(jìn)入正題!
程序
這個(gè)程序是我昨天晚上一時(shí)興起,看到了一位博主的文章 Python爬蟲實(shí)戰(zhàn)-統(tǒng)計(jì)博客園閱讀量問題 ,對(duì)他的代碼做了一些補(bǔ)充和修改。因?yàn)橄胫鼮橹庇^的展示文章數(shù)據(jù),所以分了幾個(gè)模塊去寫,以方便后續(xù)增加和修改功能
程序目前只有三個(gè) .py 文件,爬取數(shù)據(jù)后解析并寫入到 txt 中(后續(xù)會(huì)使用更規(guī)范的方法做持久化處理)
主程序 main.py
from spider import spider
from store import write_data
# 設(shè)置博客名,例如我的博客地址為:https://www.cnblogs.com/KoiC,此處則填入KoiC
blog_name = 'KoiC'
if __name__ == '__main__':
post_info = spider(blog_name)
# print(post_info)
write_data(post_info, blog_name)
print('執(zhí)行完畢!')
爬蟲模塊 spider.py
import time
import requests
import re
from lxml import etree
def spider(blog_name):
"""
爬取相關(guān)數(shù)據(jù)
"""
# 設(shè)置UA和目標(biāo)博客url
headers = {
"User-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41"
}
url = "https://www.cnblogs.com/" + blog_name + "/default.html?page=%d"
# 測(cè)試訪問
req = requests.get(url, headers)
print('測(cè)試訪問狀態(tài):%d'%req.status_code)
print('開始爬取數(shù)據(jù)...')
post_info = [] # 全部博文信息
#分頁爬取數(shù)據(jù)
for page_num in range(1, 999):
# 指向目標(biāo)url
new_url = format(url%page_num)
# 獲取頁面
req = requests.get(url=new_url, headers=headers)
# print(req.status_code)
tree = etree.HTML(req.text)
# 獲取目標(biāo)數(shù)據(jù)(各博文名稱和閱讀量)
count_list = tree.xpath('//div[@class="forFlow"]/div/div[@class="postDesc"]/span[1]/text()')
title_list = tree.xpath('//div[@class="postTitle"]/a/span/text()')
# 獲取該頁博文數(shù)量
post_count = len(count_list)
# 如果該頁沒有博文,跳出循環(huán)
if post_count == 0:
break
# 解析目標(biāo)數(shù)據(jù)
for i in range(post_count):
# 對(duì)數(shù)據(jù)進(jìn)行處理
post_title = title_list[i].strip() # 處理前后多余的空格、換行等
post_view_count = re.findall('\d+', count_list[i]) # 正則表達(dá)式獲取閱讀量數(shù)據(jù)
single_post_info = [post_title, post_view_count[0]] # 單篇博文數(shù)據(jù)
post_info.append(single_post_info)
time.sleep(0.8)
return post_info
持久化模塊 store.py
import os
import time
def write_data(post_info, blog_name):
"""
對(duì)數(shù)據(jù)進(jìn)行持久化
"""
print('開始寫入數(shù)據(jù)...')
# 獲取時(shí)間
now_time = time.localtime(time.time())
select_date = time.strftime('%Y-%m-%d', now_time)
select_time = time.strftime('%Y-%m-%d %H:%M:%S ', now_time)
# 按日期創(chuàng)建文件路徑
file_path = './{:s}/{:s}'.format(str(now_time.tm_year), str(now_time.tm_mon))
try:
os.makedirs(file_path) # 該方法創(chuàng)建路徑時(shí),若路徑存在會(huì)報(bào)異常,使用 try catch 跳過異常
except OSError:
pass
# 寫入數(shù)據(jù)
try:
fp = open('{:s}/{:s}.txt'.format(file_path, select_date), 'a+', encoding = 'utf-8')
fp.write('閱讀量\t\t 博文題目\n')
view_count = 0 # 總閱讀量
for single_post_info in post_info:
view_count += int(single_post_info[1])
fp.write('{:<12s}{:s}\n'.format(single_post_info[1], single_post_info[0]))
fp.write('------博客名:{:s} 博文數(shù)量:{:d} 總閱讀量:{:d} 統(tǒng)計(jì)時(shí)間:{:s}\n\n'.format(blog_name, len(post_info), view_count, select_time))
# 關(guān)閉資源
fp.close()
except FileNotFoundError:
print('無法打開指定的文件')
except LookupError:
print('指定編碼錯(cuò)誤')
except UnicodeDecodeError:
print('讀取文件時(shí)解碼錯(cuò)誤')
執(zhí)行結(jié)果
程序會(huì)在目錄下按日期創(chuàng)建文件夾

進(jìn)入后可找到以日期命名的 txt 文件,以我自己的博客為例,得到以下統(tǒng)計(jì)信息:

可以將程序掛在服務(wù)器上,定時(shí)統(tǒng)計(jì)數(shù)據(jù),觀察閱讀量的漲幅。
到此這篇關(guān)于基于Python實(shí)現(xiàn)文章信息統(tǒng)計(jì)的小工具 的文章就介紹到這了,更多相關(guān)Python文章信息統(tǒng)計(jì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python實(shí)現(xiàn)獲取nginx服務(wù)器ip及流量統(tǒng)計(jì)信息功能示例
- 使用python分析統(tǒng)計(jì)自己微信朋友的信息
- Python獲取統(tǒng)計(jì)自己的qq群成員信息的方法
- Python如何基于selenium實(shí)現(xiàn)自動(dòng)登錄博客園
- python讀取raw binary圖片并提取統(tǒng)計(jì)信息的實(shí)例
- Python爬取阿拉丁統(tǒng)計(jì)信息過程圖解
- Python實(shí)現(xiàn)博客快速備份的腳本分享
- Python實(shí)現(xiàn)統(tǒng)計(jì)文章閱讀量的方法詳解
相關(guān)文章
Python中使用threading.Event協(xié)調(diào)線程的運(yùn)行詳解
這篇文章主要介紹了Python中使用threading.Event協(xié)調(diào)線程的運(yùn)行詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷
這篇文章主要介紹了Python如何從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
python matplotlib.pyplot.plot()參數(shù)用法
這篇文章主要介紹了python matplotlib.pyplot.plot()參數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python?內(nèi)置函數(shù)sorted()的用法
這篇文章主要介紹了Python?內(nèi)置函數(shù)sorted()的用法,文章內(nèi)容介紹詳細(xì)具有一的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-03-03
Ubuntu16.04 安裝多個(gè)python版本的問題及解決方法
Ubuntu16.04自帶python2.7與python3.5,Ubuntu 官方 apt 庫中還未收錄 python 3.8,因此添加 deadsnakes PPA 源安裝python3.8,否則會(huì)出現(xiàn)報(bào)錯(cuò),接下來通過本文給大家介紹Ubuntu16.04 安裝python的問題,一起看看吧2021-09-09
利用python實(shí)現(xiàn).dcm格式圖像轉(zhuǎn)為.jpg格式
今天小編就為大家分享一篇利用python實(shí)現(xiàn).dcm格式圖像轉(zhuǎn)為.jpg格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
python實(shí)現(xiàn)跨年表白神器--你值得擁有
這篇文章主要介紹了python實(shí)現(xiàn)跨年表白神器的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-01-01

