Wikidata:Pywikibot - Python 3 Tutorial/Page views

Get the page views from itemsEdit

# -*- coding: utf-8  -*-
import pywikibot
import pywikibot.data.api as api
import pprint #Only for structuring the JSON file
"""
Using API calls to get pageviews
"""
site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q42")

req = api.Request(site=site, parameters={'action': 'query', #https://www.wikidata.org/w/api.php?action=query&titles=Q42&prop=pageviews
                                        'titles': item,
                                        'prop': 'pageviews'})

pprint.pprint(req.submit()['query']['pages'][str(item.pageid)]['pageviews']) #Can also use print

Result: Page view for each day.

{'2019-09-20': 132,
 '2019-09-21': 104,
  .
  .
  .
 '2019-11-02': 116,
 '2019-11-03': 122,
 '2019-11-04': 178,
 '2019-11-05': 167,
 '2019-11-06': 173,
 '2019-11-07': 170,
 '2019-11-08': 196,
 '2019-11-09': 146,
 '2019-11-10': 141,
 '2019-11-11': 143,
 '2019-11-12': 180,
 '2019-11-13': 185,
 '2019-11-14': 217,
 '2019-11-15': 167,
 '2019-11-16': 157,
 '2019-11-17': 117,
 '2019-11-18': 175}