Skip to content

Commit 3b36df0

Browse files
authored
Merge pull request #2452 from ranaroussi/fix/user-agent
Disable setting user-agent, curl_cffi does that
2 parents 2608f36 + 6f0f408 commit 3b36df0

File tree

8 files changed

+17
-35
lines changed

8 files changed

+17
-35
lines changed

yfinance/data.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import random
32
from functools import lru_cache
43

54
from curl_cffi import requests
@@ -11,7 +10,6 @@
1110
from . import utils, cache
1211
import threading
1312

14-
from .const import USER_AGENTS
1513
from .exceptions import YFRateLimitError
1614

1715
cache_maxsize = 64
@@ -66,9 +64,6 @@ class YfData(metaclass=SingletonMeta):
6664
Have one place to retrieve data from Yahoo API in order to ease caching and speed up operations.
6765
Singleton means one session one cookie shared by all threads.
6866
"""
69-
user_agent_headers = {
70-
'User-Agent': random.choice(USER_AGENTS)
71-
}
7267

7368
def __init__(self, session=None, proxy=None):
7469
self._crumb = None
@@ -85,8 +80,6 @@ def __init__(self, session=None, proxy=None):
8580
self._set_session(session or requests.Session(impersonate="chrome"))
8681
self._set_proxy(proxy)
8782

88-
utils.get_yf_logger().debug(f"Using User-Agent: {self.user_agent_headers['User-Agent']}")
89-
9083
def _set_session(self, session):
9184
if session is None:
9285
return
@@ -178,7 +171,6 @@ def _get_cookie_basic(self, timeout=30):
178171
# - 'allow_redirects' copied from @psychoz971 solution - does it help USA?
179172
self._session.get(
180173
url='https://fc.yahoo.com',
181-
headers=self.user_agent_headers,
182174
timeout=timeout,
183175
allow_redirects=True)
184176

@@ -191,7 +183,6 @@ def _get_crumb_basic(self, timeout=30):
191183
# - 'allow_redirects' copied from @psychoz971 solution - does it help USA?
192184
get_args = {
193185
'url': "https://query1.finance.yahoo.com/v1/test/getcrumb",
194-
'headers': self.user_agent_headers,
195186
'timeout': timeout,
196187
'allow_redirects': True
197188
}
@@ -225,7 +216,6 @@ def _get_cookie_csrf(self, timeout):
225216
return True
226217

227218
base_args = {
228-
'headers': self.user_agent_headers,
229219
'timeout': timeout}
230220

231221
get_args = {**base_args, 'url': 'https://guce.yahoo.com/consent'}
@@ -297,7 +287,6 @@ def _get_crumb_csrf(self, timeout=30):
297287

298288
get_args = {
299289
'url': 'https://query2.finance.yahoo.com/v1/test/getcrumb',
300-
'headers': self.user_agent_headers,
301290
'timeout': timeout}
302291
if self._session_is_caching:
303292
get_args['expire_after'] = self._expire_after
@@ -337,15 +326,15 @@ def _get_cookie_and_crumb(self, timeout=30):
337326
return crumb, strategy
338327

339328
@utils.log_indent_decorator
340-
def get(self, url, user_agent_headers=None, params=None, timeout=30):
341-
return self._make_request(url, request_method = self._session.get, user_agent_headers=user_agent_headers, params=params, timeout=timeout)
329+
def get(self, url, params=None, timeout=30):
330+
return self._make_request(url, request_method = self._session.get, params=params, timeout=timeout)
342331

343332
@utils.log_indent_decorator
344-
def post(self, url, body, user_agent_headers=None, params=None, timeout=30):
345-
return self._make_request(url, request_method = self._session.post, user_agent_headers=user_agent_headers, body=body, params=params, timeout=timeout)
333+
def post(self, url, body, params=None, timeout=30):
334+
return self._make_request(url, request_method = self._session.post, body=body, params=params, timeout=timeout)
346335

347336
@utils.log_indent_decorator
348-
def _make_request(self, url, request_method, user_agent_headers=None, body=None, params=None, timeout=30):
337+
def _make_request(self, url, request_method, body=None, params=None, timeout=30):
349338
# Important: treat input arguments as immutable.
350339

351340
if len(url) > 200:
@@ -368,8 +357,7 @@ def _make_request(self, url, request_method, user_agent_headers=None, body=None,
368357
request_args = {
369358
'url': url,
370359
'params': {**params, **crumbs},
371-
'timeout': timeout,
372-
'headers': user_agent_headers or self.user_agent_headers
360+
'timeout': timeout
373361
}
374362

375363
if body:
@@ -396,11 +384,11 @@ def _make_request(self, url, request_method, user_agent_headers=None, body=None,
396384

397385
@lru_cache_freezeargs
398386
@lru_cache(maxsize=cache_maxsize)
399-
def cache_get(self, url, user_agent_headers=None, params=None, timeout=30):
400-
return self.get(url, user_agent_headers, params, timeout)
387+
def cache_get(self, url, params=None, timeout=30):
388+
return self.get(url, params, timeout)
401389

402-
def get_raw_json(self, url, user_agent_headers=None, params=None, timeout=30):
390+
def get_raw_json(self, url, params=None, timeout=30):
403391
utils.get_yf_logger().debug(f'get_raw_json(): {url}')
404-
response = self.get(url, user_agent_headers=user_agent_headers, params=params, timeout=timeout)
392+
response = self.get(url, params=params, timeout=timeout)
405393
response.raise_for_status()
406394
return response.json()

yfinance/domain/domain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _fetch(self, query_url) -> Dict:
122122
Dict: The JSON response data from the request.
123123
"""
124124
params_dict = {"formatted": "true", "withReturns": "true", "lang": "en-US", "region": "US"}
125-
result = self._data.get_raw_json(query_url, user_agent_headers=self._data.user_agent_headers, params=params_dict)
125+
result = self._data.get_raw_json(query_url, params=params_dict)
126126
return result
127127

128128
def _parse_and_assign_common(self, data) -> None:

yfinance/scrapers/analysis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def _fetch(self, modules: list):
177177
raise YFException("No valid modules provided, see available modules using `valid_modules`")
178178
params_dict = {"modules": modules, "corsDomain": "finance.yahoo.com", "formatted": "false", "symbol": self._symbol}
179179
try:
180-
result = self._data.get_raw_json(_QUOTE_SUMMARY_URL_ + f"/{self._symbol}", user_agent_headers=self._data.user_agent_headers, params=params_dict)
180+
result = self._data.get_raw_json(_QUOTE_SUMMARY_URL_ + f"/{self._symbol}", params=params_dict)
181181
except requests.exceptions.HTTPError as e:
182182
utils.get_yf_logger().error(str(e))
183183
return None

yfinance/scrapers/funds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _fetch(self):
175175
"""
176176
modules = ','.join(["quoteType", "summaryProfile", "topHoldings", "fundProfile"])
177177
params_dict = {"modules": modules, "corsDomain": "finance.yahoo.com", "symbol": self._symbol, "formatted": "false"}
178-
result = self._data.get_raw_json(_QUOTE_SUMMARY_URL_+self._symbol, user_agent_headers=self._data.user_agent_headers, params=params_dict)
178+
result = self._data.get_raw_json(_QUOTE_SUMMARY_URL_+self._symbol, params=params_dict)
179179
return result
180180

181181
def _fetch_and_parse(self) -> None:

yfinance/scrapers/holders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _fetch(self):
6767
modules = ','.join(
6868
["institutionOwnership", "fundOwnership", "majorDirectHolders", "majorHoldersBreakdown", "insiderTransactions", "insiderHolders", "netSharePurchaseActivity"])
6969
params_dict = {"modules": modules, "corsDomain": "finance.yahoo.com", "formatted": "false"}
70-
result = self._data.get_raw_json(f"{_QUOTE_SUMMARY_URL_}/{self._symbol}", user_agent_headers=self._data.user_agent_headers, params=params_dict)
70+
result = self._data.get_raw_json(f"{_QUOTE_SUMMARY_URL_}/{self._symbol}", params=params_dict)
7171
return result
7272

7373
def _fetch_and_parse(self):

yfinance/scrapers/quote.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def _fetch(self, modules: list):
587587
raise YFException("No valid modules provided, see available modules using `valid_modules`")
588588
params_dict = {"modules": modules, "corsDomain": "finance.yahoo.com", "formatted": "false", "symbol": self._symbol}
589589
try:
590-
result = self._data.get_raw_json(_QUOTE_SUMMARY_URL_ + f"/{self._symbol}", user_agent_headers=self._data.user_agent_headers, params=params_dict)
590+
result = self._data.get_raw_json(_QUOTE_SUMMARY_URL_ + f"/{self._symbol}", params=params_dict)
591591
except requests.exceptions.HTTPError as e:
592592
utils.get_yf_logger().error(str(e))
593593
return None
@@ -596,9 +596,7 @@ def _fetch(self, modules: list):
596596
def _fetch_additional_info(self):
597597
params_dict = {"symbols": self._symbol, "formatted": "false"}
598598
try:
599-
result = self._data.get_raw_json(f"{_QUERY1_URL_}/v7/finance/quote?",
600-
user_agent_headers=self._data.user_agent_headers,
601-
params=params_dict)
599+
result = self._data.get_raw_json(f"{_QUERY1_URL_}/v7/finance/quote?", params=params_dict)
602600
except requests.exceptions.HTTPError as e:
603601
utils.get_yf_logger().error(str(e))
604602
return None
@@ -679,7 +677,7 @@ def _fetch_complementary(self):
679677
# p = _re.compile(r'root\.App\.main = (.*);')
680678
# url = 'https://finance.yahoo.com/quote/{}/key-statistics?p={}'.format(self._ticker.ticker, self._ticker.ticker)
681679
# try:
682-
# r = session.get(url, headers=utils.user_agent_headers)
680+
# r = session.get(url)
683681
# data = _json.loads(p.findall(r.text)[0])
684682
# key_stats = data['context']['dispatcher']['stores']['QuoteTimeSeriesStore']["timeSeries"]
685683
# for k in keys:

yfinance/screener/screener.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ def screen(query: Union[str, EquityQuery, FundQuery],
190190
# Fetch
191191
response = _data.post(_SCREENER_URL_,
192192
body=post_query,
193-
user_agent_headers=_data.user_agent_headers,
194193
params=params_dict)
195194
response.raise_for_status()
196195
return response.json()['finance']['result'][0]

yfinance/utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040

4141
from yfinance import const
4242

43-
user_agent_headers = {
44-
'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'}
45-
4643
# From https://stackoverflow.com/a/59128615
4744
def attributes(obj):
4845
disallowed_names = {

0 commit comments

Comments
 (0)