Created
January 15, 2019 15:59
-
-
Save windows98SE/da0f08df55db705193b42775799cb5f6 to your computer and use it in GitHub Desktop.
fetch yahoo finance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
import requests | |
session = requests.Session() | |
stock = 'T' | |
start = '1547398800' | |
end = '1547485200' | |
interval = '1d' | |
# step 1 | |
# (find cookies & crumb) | |
req = session.get('https://finance.yahoo.com/quote/'+ stock.upper() +'/history') | |
cookies = req.cookies['B'] | |
crumb = re.findall('"CrumbStore":{"crumb":"([^"]+)"}', req.text)[0] | |
# debug | |
# print(cookies, crumb.encode('ascii').decode('unicode-escape')) | |
# step 2 | |
# download | |
download_url = ('https://query1.finance.yahoo.com/v7/finance/download/{}?' | |
'period1={}' | |
'&period2={}' | |
'&interval={}' | |
'&events=history' | |
'&crumb={}').format(stock, start, end, interval, crumb.encode('ascii').decode('unicode-escape')) | |
data = session.get(download_url, cookies={'B':cookies}) | |
print(data.text) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment