基于Python實現(xiàn)文章信息統(tǒng)計的小工具
前言
博客園在個人首頁有一個簡單的博客數(shù)據(jù)統(tǒng)計,以博客園官方的首頁為例:
但是這些數(shù)據(jù)不足以分析更為細節(jié)的東西
起初我是想把博客園作為個人學習的云筆記,但在一點點的記錄中,我逐漸把博客園視為知識創(chuàng)作和知識分享的平臺
所以從年后開始,就想著做一個類似 CSDN 里統(tǒng)計文章數(shù)據(jù)的工具
這樣的統(tǒng)計功能可以更好的去分析讀者對于內(nèi)容的需求,了解文章內(nèi)容的價值,以及從側面認識自己在知識創(chuàng)作方面的能力
說了不少無關的話,下面直接進入正題!
程序
這個程序是我昨天晚上一時興起,看到了一位博主的文章 Python爬蟲實戰(zhàn)-統(tǒng)計博客園閱讀量問題 ,對他的代碼做了一些補充和修改。因為想著要更為直觀的展示文章數(shù)據(jù),所以分了幾個模塊去寫,以方便后續(xù)增加和修改功能
程序目前只有三個 .py 文件,爬取數(shù)據(jù)后解析并寫入到 txt 中(后續(xù)會使用更規(guī)范的方法做持久化處理)
主程序 main.py
from spider import spider from store import write_data # 設置博客名,例如我的博客地址為: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): """ 爬取相關數(shù)據(jù) """ # 設置UA和目標博客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" # 測試訪問 req = requests.get(url, headers) print('測試訪問狀態(tài):%d'%req.status_code) print('開始爬取數(shù)據(jù)...') post_info = [] # 全部博文信息 #分頁爬取數(shù)據(jù) for page_num in range(1, 999): # 指向目標url new_url = format(url%page_num) # 獲取頁面 req = requests.get(url=new_url, headers=headers) # print(req.status_code) tree = etree.HTML(req.text) # 獲取目標數(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 # 解析目標數(shù)據(jù) for i in range(post_count): # 對數(shù)據(jù)進行處理 post_title = title_list[i].strip() # 處理前后多余的空格、換行等 post_view_count = re.findall('\d+', count_list[i]) # 正則表達式獲取閱讀量數(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): """ 對數(shù)據(jù)進行持久化 """ print('開始寫入數(shù)據(jù)...') # 獲取時間 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)建路徑時,若路徑存在會報異常,使用 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)計時間:{:s}\n\n'.format(blog_name, len(post_info), view_count, select_time)) # 關閉資源 fp.close() except FileNotFoundError: print('無法打開指定的文件') except LookupError: print('指定編碼錯誤') except UnicodeDecodeError: print('讀取文件時解碼錯誤')
執(zhí)行結果
程序會在目錄下按日期創(chuàng)建文件夾
進入后可找到以日期命名的 txt 文件,以我自己的博客為例,得到以下統(tǒng)計信息:
可以將程序掛在服務器上,定時統(tǒng)計數(shù)據(jù),觀察閱讀量的漲幅。
到此這篇關于基于Python實現(xiàn)文章信息統(tǒng)計的小工具 的文章就介紹到這了,更多相關Python文章信息統(tǒng)計內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)從文件中讀取數(shù)據(jù)并繪制成 x y 軸圖形的方法
今天小編就為大家分享一篇python實現(xiàn)從文件中讀取數(shù)據(jù)并繪制成 x y 軸圖形的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10web.py 十分鐘創(chuàng)建簡易博客實現(xiàn)代碼
web.py是一款輕量級的Python web開發(fā)框架,簡單、高效、學習成本低,特別適合作為python web開發(fā)的入門框架2016-04-04Python中DataFrame與內(nèi)置數(shù)據(jù)結構相互轉換的實現(xiàn)
pandas?支持我們從?Excel、CSV、數(shù)據(jù)庫等不同數(shù)據(jù)源當中讀取數(shù)據(jù),來構建?DataFrame。但有時數(shù)據(jù)并不來自這些外部數(shù)據(jù)源,這就涉及到了?DataFrame?和?Python?內(nèi)置數(shù)據(jù)結構之間的相互轉換,本文就來和大家詳細聊聊2023-02-02Python 實現(xiàn) WebSocket 通信的過程詳解
WebSocket是一種在Web應用程序中實現(xiàn)雙向通信的協(xié)議,與傳統(tǒng)的HTTP請求-響應模型不同,WebSocket允許服務器主動向客戶端推送數(shù)據(jù),實現(xiàn)實時性和互動性,這篇文章主要介紹了Python 實現(xiàn) WebSocket 通信的過程詳解,需要的朋友可以參考下2024-06-06