Home > Back-end >  Yahoo Finance encrypt context Python fetch
Yahoo Finance encrypt context Python fetch

Time:12-18

Using python script to find quotes data but yahoo recently encrypted it, now it is not clear text quotes but it shows no encrypt when web browser.

def _get_headers():
return {"accept": "text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
"authority":"ca.finance.yahoo.com",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en;q=0.9",
"cache-control": "no-cache",
"dnt": "1",
"sec-ch-ua-platform": "Windows",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}

def get_yahoo_finance_price(ticker):
time.sleep(5)
url = 'https://finance.yahoo.com/quote/' ticker '/history?p=' ticker

html = requests.get(url,  headers=_get_headers(), timeout=(3.05, 21)).text

soup = BeautifulSoup(html,'html.parser')
soup_script = soup.find("script",text=re.compile("root.App.main")).text
matched = re.search("root.App.main\s =\s (\{.*\})",soup_script)
# if matched:
json_script = json.loads(matched.group(1))
print(json_script)
data = json_script['context']['dispatcher']['stores']['HistoricalPriceStore']['prices'][0]
df = pd.DataFrame({'date': dt.fromtimestamp(data['date']).strftime("%Y-%m-%d"),
                 'close': round(data['close'], 2),
                 "adjusted close": round(data['adjclose'], 2),
                 'volume': data['volume'],
                 'open': round(data['open'], 2),
                 'high': round(data['high'], 2),
                 'low': round(data['low'], 2),
                 }, index=[0])
return df

CodePudding user response:

Try it this way.

import yfinance as yf

# Get the data for the stock AAPL
start = '2019-06-30'
end = '2020-06-30'

data = yf.download('SBUX', start, end)


data = data.reset_index()
data

data.dtypes

CodePudding user response:

same problem here.

Found a hint how to solve, but the example is in JS using crypto-js: https://github.com/ranaroussi/yfinance/issues/1246#issuecomment-1356290247

need to implement it with pycryptodome probably.

  • Related