Skip to content

Instantly share code, notes, and snippets.

@fduxiao
Created July 27, 2017 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fduxiao/5e07c80e9ff7672f8979f02dc07bb2da to your computer and use it in GitHub Desktop.
Save fduxiao/5e07c80e9ff7672f8979f02dc07bb2da to your computer and use it in GitHub Desktop.
Yahoo Finance crawler
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