Skip to content

Commit

Permalink
Randomly pick User-Agent header on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvan2006 committed Feb 18, 2025
1 parent 1ddc38c commit 95b59a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions yfinance/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,21 @@ def merge_two_level_dicts(dict1, dict2):
"highest_controversy"}
}
EQUITY_SCREENER_FIELDS = merge_two_level_dicts(EQUITY_SCREENER_FIELDS, COMMON_SCREENER_FIELDS)

USER_AGENTS = [
# Chrome
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",

# Firefox
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14.7; rv:135.0) Gecko/20100101 Firefox/135.0",
"Mozilla/5.0 (X11; Linux i686; rv:135.0) Gecko/20100101 Firefox/135.0",

# Safari
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15",

# Edge
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/131.0.2903.86"
]
5 changes: 4 additions & 1 deletion yfinance/data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import random
from functools import lru_cache

import requests as requests
Expand All @@ -10,6 +11,7 @@
from . import utils, cache
import threading

from .const import USER_AGENTS
from .exceptions import YFRateLimitError

cache_maxsize = 64
Expand Down Expand Up @@ -59,7 +61,8 @@ class YfData(metaclass=SingletonMeta):
Singleton means one session one cookie shared by all threads.
"""
user_agent_headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
'User-Agent': random.choice(USER_AGENTS)
}

def __init__(self, session=None):
self._crumb = None
Expand Down

0 comments on commit 95b59a7

Please sign in to comment.