****************************** Smarter Scraping with Caching ****************************** Install the `nospam` package to cache API calls and reduce spam to Yahoo: .. code-block:: bash pip install yfinance[nospam] To use a custom `requests` session, pass a `session=` argument to the Ticker constructor. This allows for caching calls to the API as well as a custom way to modify requests via the `User-agent` header. .. code-block:: python import requests_cache session = requests_cache.CachedSession('yfinance.cache') session.headers['User-agent'] = 'my-program/1.0' ticker = yf.Ticker('MSFT', session=session) # The scraped response will be stored in the cache ticker.actions Combine `requests_cache` with rate-limiting to avoid triggering Yahoo's rate-limiter/blocker that can corrupt data. .. code-block:: python from requests import Session from requests_cache import CacheMixin, SQLiteCache from requests_ratelimiter import LimiterMixin, MemoryQueueBucket from pyrate_limiter import Duration, RequestRate, Limiter class CachedLimiterSession(CacheMixin, LimiterMixin, Session): pass session = CachedLimiterSession( limiter=Limiter(RequestRate(2, Duration.SECOND*5)), # max 2 requests per 5 seconds bucket_class=MemoryQueueBucket, backend=SQLiteCache("yfinance.cache"), )