Created
July 27, 2017 15:25
-
-
Save fduxiao/5e07c80e9ff7672f8979f02dc07bb2da to your computer and use it in GitHub Desktop.
Yahoo Finance crawler
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
import pandas as pd | |
import requests | |
import time | |
def get_data(ticker, start_date, end_date, delta="1d"): | |
data_url = "https://query2.finance.yahoo.com/v8/finance/chart/" + ticker | |
params = dict() | |
params['interval'] = delta | |
params['period1'] = int(time.mktime(start_date.timetuple())) | |
params['period2'] = int(time.mktime(end_date.timetuple())) | |
r = requests.get(data_url, params=params) | |
j = r.json() | |
data = j['chart'] | |
if data['error'] is not None: | |
print(data['error']) | |
exit(0) | |
if len(data['result']) == 0: | |
print('empty') | |
return None | |
data = data['result'][0] | |
dates = pd.to_datetime(data['timestamp'], unit='s') | |
# dict_keys(['meta', 'timestamp', 'indicators']) | |
indicators = data['indicators'] | |
quote = indicators['quote'][0] | |
# quote['aclose'] = quote.pop('close') | |
# unadjclose = indicators['unadjclose'][0]['unadjclose'] | |
# quote['close'] = unadjclose | |
df = pd.DataFrame(data=quote, index=dates) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment