Skip to content
Steve edited this page Jan 20, 2018 · 5 revisions

Caching is mentioned briefly in Request actions, and more fully in issue #103.

Requests are cached in the cache attribute of each instance:

  • page.cache
  • category.cache
  • site.cache

The cache attribute contains:

  1. query: the request URL
  2. info: information about the request
  3. response: the response body as dict or str

You can access the cache directly (e.g. page.cache), or you can use the convenience methods page.query(), page.info(), and page.response():

>>> page = wptools.page()
>>> page.get()

>>> page.query()
['imageinfo', 'restbase', 'labels', 'parse', 'wikidata', 'query']
>>> page.query('wikidata')
u'https://www.wikidata.org/w/api.php?action=wbgetentities&formatversion=2&languages=en&props=aliases|info|claims|descriptions|labels|sitelinks&redirects=yes&ids=Q1001'
>>> page.info('wikidata')
{'bytes': 102421.0,
 'content': 'application/json; charset=utf-8',
 'kB/s': '110.5',
 'seconds': '0.927',
 'status': 200,
 'url': 'https://www.wikidata.org/w/api.php?action=wbgetentities&languages=en&props=aliases|info|claims|descriptions|labels|sitelinks&redirects=yes&ids=Q1001',
 'user-agent': 'wptools/0.4.8 (https://github.com/siznax/wptools) PycURL/7.43.0.1 libcurl/7.54.0 LibreSSL/2.0.20 zlib/1.2.11 nghttp2/1.24.0'}
>>> page.response('wikidata')
{u'entities': {u'Q1001': {u'aliases': {u'en': [{u'language': u'en',
      u'value': u'M K Gandhi'},
     {u'language': u'en', u'value': u'Mohandas Gandhi'},
     {u'language': u'en', u'value': u'Bapu'},
     {u'language': u'en', u'value': u'Gandhi'},
...
Clone this wiki locally