I'm using Wikipedia's Pageviews API in Python and I'm trying to get time and views for a list of pages. My idea was to create a dictionary with the wikipedia page (eg. Rome) as key and all the attributes as a dict of values. Then I want to create a dict with the wikipedia page name as the name of the dict and for the key the time and for the value the views. I can't create one dynamically, however.
My code is:
dfs = []
import pageviewapi
emptylist = []
dictrome = {}
dictparis = {}
dct0 = {}
list = ['Paris', 'Rome']
for x in list:
dct0[x] = pageviewapi.per_article('it.wikipedia', x, '20150101', '20210101',
access='all-access', agent='all-agents', granularity='monthly')
if 'Rome' in dct0:
for i in dct0.values():
for p in range(0,len(i['items'])):
dictrome[(i['items'][p]['timestamp'])] = (i['items'][p]['views'])
else:
for i in dct0.values():
for p in range(0,len(i['items'])):
dictparis[(i['items'][p]['timestamp'])] = (i['items'][p]['views'])
And the output I want, which is the one I have, is:
dictrome: {'2015070100': 890, '2015080100': 879, '2015090100': 971, '2015100100': 1097, '2015110100': 2259}
dictparis : {'2015070100': 482, '2015080100': 467, '2015090100': 371, '2015100100': 425, '2015110100': 408}
I just want to automate that "if" condition, because the pages in my list will be hundreds.