用戶:VectorST/chartCreator
跳至導覽
跳至搜尋
簡介
本代碼配合Template:Echart食用。
我代碼水平不高,可能代碼存在冗餘等問題。
萌娘百科對於爬取速度有限制我被伺服器封了三次了,保險起見設置為獲取每月數據後休眠 10s,如果頭鐵想提高速度可以修改 Line 42 time.sleep(10)。
對於編輯量為 0 的月份默認不錄入,如果你想錄入請刪掉 Line 32 的 if contributions!=0 : 判斷條件。
食用方法
修改 user 和 year 參數,使用 Python 運行,程序運行完成後會自動在相同目錄生成文件 dataMoegirl.json 。
已知問題
- Line 43
if mark : break不知為何不起作用,導致強行輸出到本年12月。 - 受萌娘百科API限制,每個月最多可以查到500條,如果你當月編輯數量大於500,只會囿於限制而顯示500條。
代碼
# -*- coding:utf-8 -*-
import requests
import json
import calendar
import datetime
import time
user = "VectorST" #用户名
year = 2020 #起始年份
nowTime = datetime.datetime.now()
timeData = []
contributionsData = []
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0"
}
mark = False
while year <= nowTime.year:
x = ['01','02','03','04','05','06','07','08','09','10','11','12']
for month in x:
endDay = calendar.monthrange(year, (int)(month))[1] #当月结束日
startTime = f'{year}{month}01000000'
if year == nowTime.year and month == nowTime.month: mark = True
endTime = time.strftime("%Y%m%d%H%M%S") if mark else f'{year}{month}{endDay}235959'
url ="https://zh.moegirl.org.cn/api.php?action=query&format=json&list=usercontribs&ucuser="+user+"&ucdir=newer&uclimit=500&ucstart="+startTime+"&ucend="+endTime
rawData = requests.get(url, headers=headers)
jsonData = json.loads(rawData.text)
contributions = len(jsonData['query']['usercontribs'])
if contributions!=0 :
timeData.append(f'{year}年{month}月')
contributionsDataAppend = {
"value": contributions,
"label": {
"show": True
}
}
contributionsData.append(contributionsDataAppend)
print(f'{year}年{month}月:{contributions}')
time.sleep(10)
if mark : break
year += 1
today = f'{nowTime.year} 年 {nowTime.month} 月 {nowTime.day} 日 {nowTime.hour}:{nowTime.minute}'
jsonContent = {
"title": {
"text": "编辑历史",
"subtext": f'按月统计(截至 {today} )',
"subtextStyle": {
"color": "#333"
}
},
"tooltip": {
"trigger": "axis",
"axisPointer": {
"type": "cross",
"animation": False
}
},
"toolbox": {
"show": True,
"feature": {
"dataZoom": {
"yAxisIndex": "none"
},
"dataView": {
"readOnly": True
},
"magicType": {
"type": [
"line",
"bar"
]
},
"restore": {},
"saveAsImage": {
"excludeComponents": [
"toolbox",
"dataZoom"
]
}
}
},
"axisPointer": {
"link": {
"xAxisIndex": "all"
}
},
"dataZoom": [
{
"type": "inside",
"xAxisIndex": [
0
],
"startValue": 0,
"end": 100
},
{
"show": True,
"xAxisIndex": [
0
],
"type": "slider",
"start": 0,
"end": 100
}
],
"legend": {
"data": [
"编辑次数"
]
},
"xAxis": {
"data": timeData
},
"yAxis": {},
"series": [
{
"name": "每月编辑数",
"type": "line",
"label": {
"formatter": "{c}",
"distance": 0,
"backgroundColor": "#fff",
"padding": 1
},
"data": contributionsData,
"markPoint": {
"data": [
{
"type": "max",
"name": "最大值"
},
{
"type": "min",
"name": "最小值"
}
]
},
"markLine": {
"data": [
{
"type": "average",
"name": "平均值"
}
]
}
}
],
"animation": False
}
f = open("dataMoegirl.json", "a")
f.write(json.dumps(jsonContent))
f.close()