@@ -34,7 +34,10 @@ class YahooDailyReader(_DailyBaseReader):
34
34
Time, in seconds, to pause between consecutive queries of chunks. If
35
35
single value given for symbol, represents the pause between retries.
36
36
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.
38
41
adjust_price : bool, default False
39
42
If True, adjusts all prices in hist_data ('Open', 'High', 'Low',
40
43
'Close') based on 'Adj Close' price. Adds 'Adj_Ratio' column and drops
@@ -50,6 +53,9 @@ class YahooDailyReader(_DailyBaseReader):
50
53
If True, adds Dividend and Split columns to dataframe.
51
54
adjust_dividends: bool, default true
52
55
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.
53
59
"""
54
60
55
61
def __init__ (
@@ -66,6 +72,7 @@ def __init__(
66
72
interval = "d" ,
67
73
get_actions = False ,
68
74
adjust_dividends = True ,
75
+ headers = None ,
69
76
):
70
77
super (YahooDailyReader , self ).__init__ (
71
78
symbols = symbols ,
@@ -80,17 +87,21 @@ def __init__(
80
87
# Ladder up the wait time between subsequent requests to improve
81
88
# probability of a successful retry
82
89
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
94
105
95
106
self .adjust_price = adjust_price
96
107
self .ret_index = ret_index
@@ -150,7 +161,7 @@ def _read_one_data(self, url, params):
150
161
del params ["symbol" ]
151
162
url = url .format (symbol )
152
163
153
- resp = self ._get_response (url , params = params )
164
+ resp = self ._get_response (url , params = params , headers = self . headers )
154
165
ptrn = r"root\.App\.main = (.*?);\n}\(this\)\);"
155
166
try :
156
167
j = json .loads (re .search (ptrn , resp .text , re .DOTALL ).group (1 ))
0 commit comments