本代码配合Template:Echart食用。
我代码水平不高,可能代码存在冗余等问题。
萌娘百科对于爬取速度有限制我被服务器封了三次了,保险起见设置为获取每月数据后休眠 10s,如果头铁想提高速度可以修改 Line 42 time.sleep(10)
。
对于编辑量为 0 的月份默认不录入,如果你想录入请删掉 Line 32 的 if contributions!=0 :
判断条件。
修改 user 和 year 参数,使用 Python 运行,程序运行完成后会自动在相同目录生成文件 dataMoegirl.json 。
if mark : break
不知为何不起作用,导致强行输出到本年12月。
# -*- 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()