import pandas as pd
import requests
import json
import matplotlib.pyplot as plt
api = 'https://wikimedia.org/api/rest_v1/metrics/pageviews/top-by-country/en.wikipedia/all-access/'
headers = {'User-Agent': 'extracting about 75 statistics api calls'}
views = []
for date in pd.date_range(start='2017-01', end='2023-03', freq='MS'):
resp = requests.get(api + date.strftime('%Y/%m'), headers=headers).text
total = 0
for country in json.loads(resp)['items'][0]['countries']:
if country['country'] in ('US', 'UK'):
total += country['views_ceil']
views.append((date, total))
df = pd.DataFrame(views, columns=['month', 'billions'])
df = df.set_index('month')/1e9 # billions
df.plot(ylim=(0, None), title="Monthly US and UK English Wikipedia direct page views")
plt.show()
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
to share – to copy, distribute and transmit the work
to remix – to adapt the work
Under the following conditions:
attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.