Skip to content

Commit b56ca89

Browse files
authored
Merge pull request #883 from bashtage/galashour-master
BUG: Correct behavior of yahoo
2 parents 6d6239d + b16d7a7 commit b56ca89

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

pandas_datareader/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class _BaseReader(object):
3838
pause : float, default 0.1
3939
Time, in seconds, of the pause between retries.
4040
session : Session, default None
41-
requests.sessions.Session instance to be used
41+
requests.sessions.Session instance to be used.
4242
freq : {str, None}
4343
Frequency to use in select readers
4444
"""
@@ -72,6 +72,7 @@ def __init__(
7272
self.pause_multiplier = 1
7373
self.session = _init_session(session, retry_count)
7474
self.freq = freq
75+
self.headers = None
7576

7677
def close(self):
7778
"""Close network session"""
@@ -148,7 +149,10 @@ def _get_response(self, url, params=None, headers=None):
148149
parameters passed to the URL
149150
"""
150151

151-
# initial attempt + retry
152+
# Use default headers if not passes and not using a user session
153+
if headers is None:
154+
headers = self.headers
155+
152156
pause = self.pause
153157
last_response_text = ""
154158
for _ in range(self.retry_count + 1):

pandas_datareader/yahoo/daily.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)