Do not generate communication every time you create a DataFrame from a web resource with read_json
I want to get information using web api and do various things, but there is a limit to the number of requests, and if I access it every time, I immediately get caught in the upper limit. I don't need real-time information when writing test code, so I want to keep it locally in the cache.
import pandas as pd
import requests
import requests_cache
from io import StringIO
requests_cache.install_cache()
response = requests.get("https://financialmodelingprep.com/api/v3/income-statement/AAPL?apikey=demo")
content = response.content.decode()
df = pd.read_json(StringIO(content))
requests_cache
If the cache exists when fetching with request.get
, communication is not performed and response is returned from the cache.
You can change the cache name with requests_cache.install_cache ("test ")
and manage each one.
Caching is done with sqlite. You can delete the cache by deleting the file.
StringIO
Since the file name and URL are specified in pd.read_json
, the str
obtained by requests cannot be specified as it is.
By using StringIO (content)
, you can treat str
like a file.
Recommended Posts