@@ -34,7 +34,10 @@ class YahooDailyReader(_DailyBaseReader):
3434 Time, in seconds, to pause between consecutive queries of chunks. If
3535 single value given for symbol, represents the pause between retries.
3636 session : Session, default None
37- requests.sessions.Session instance to be used
37+ requests.sessions.Session instance to be used. Passing a session
38+ is an advanced usage and you must either set the required
39+ headers in the session directly or explicitly override
40+ using the ``headers`` argument.
3841 adjust_price : bool, default False
3942 If True, adjusts all prices in hist_data ('Open', 'High', 'Low',
4043 'Close') based on 'Adj Close' price. Adds 'Adj_Ratio' column and drops
@@ -50,6 +53,9 @@ class YahooDailyReader(_DailyBaseReader):
5053 If True, adds Dividend and Split columns to dataframe.
5154 adjust_dividends: bool, default true
5255 If True, adjusts dividends for splits.
56+ headers : dict, optional
57+ Headers to use when reading data. If None (the default), a
58+ standard set of headers is used.
5359 """
5460
5561 def __init__ (
@@ -66,6 +72,7 @@ def __init__(
6672 interval = "d" ,
6773 get_actions = False ,
6874 adjust_dividends = True ,
75+ headers = None ,
6976 ):
7077 super (YahooDailyReader , self ).__init__ (
7178 symbols = symbols ,
@@ -80,17 +87,21 @@ def __init__(
8087 # Ladder up the wait time between subsequent requests to improve
8188 # probability of a successful retry
8289 self .pause_multiplier = 2.5
83-
84- self .headers = {
85- "Connection" : "keep-alive" ,
86- "Expires" : str (- 1 ),
87- "Upgrade-Insecure-Requests" : str (1 ),
88- # Google Chrome:
89- "User-Agent" : (
90- "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
91- "(KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
92- ),
93- }
90+ if headers is not None :
91+ self .headers = headers
92+ elif session is None :
93+ self .headers = {
94+ "Connection" : "keep-alive" ,
95+ "Expires" : str (- 1 ),
96+ "Upgrade-Insecure-Requests" : str (1 ),
97+ # Google Chrome:
98+ "User-Agent" : (
99+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
100+ "(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
101+ ),
102+ }
103+ else :
104+ self .headers = None
94105
95106 self .adjust_price = adjust_price
96107 self .ret_index = ret_index
@@ -150,7 +161,7 @@ def _read_one_data(self, url, params):
150161 del params ["symbol" ]
151162 url = url .format (symbol )
152163
153- resp = self ._get_response (url , params = params )
164+ resp = self ._get_response (url , params = params , headers = self . headers )
154165 ptrn = r"root\.App\.main = (.*?);\n}\(this\)\);"
155166 try :
156167 j = json .loads (re .search (ptrn , resp .text , re .DOTALL ).group (1 ))
0 commit comments