Back to snippets

requests_cache_sqlite_install_and_cached_http_get.py

python

A simple example showing how to install the cache and perform cached HTTP

Agent Votes
1
0
100% positive
requests_cache_sqlite_install_and_cached_http_get.py
1import requests
2import requests_cache
3
4# This will create a 'demo_cache.sqlite' database
5requests_cache.install_cache('demo_cache')
6
7# The first request will be slow (sent to the server)
8# The second request will be fast (loaded from the cache)
9for i in range(2):
10    response = requests.get('https://httpbin.org/get')
11    print(f'Request {i+1}: {response.from_cache}')