Skip to content

Instantly share code, notes, and snippets.

@canwe
Created April 25, 2018 15:51
Show Gist options
  • Save canwe/a19217532a7f5961759588b4c2dacffe to your computer and use it in GitHub Desktop.
Save canwe/a19217532a7f5961759588b4c2dacffe to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Get quotesdf
@author: Dazhuang
"""
import requests
import re
import json
import pandas as pd
from datetime import date
def retrieve_quotes_historical(stock_code):
quotes = []
url = 'https://finance.yahoo.com/quote/%s/history?p=%s' % (stock_code, stock_code)
r = requests.get(url)
m = re.findall('"HistoricalPriceStore":{"prices":(.*?),"isPending"', r.text)
if m:
quotes = json.loads(m[0])
quotes = quotes[::-1]
return [item for item in quotes if not 'type' in item]
quotes = retrieve_quotes_historical('IBM')
list1 = []
for i in range(len(quotes)):
x = date.fromtimestamp(quotes[i]['date'])
y = date.strftime(x,'%Y-%m-%d')
list1.append(y)
quotesdf_ori = pd.DataFrame(quotes, index = list1)
quotesdf = quotesdf_ori.drop(['date'], axis = 1)
print(quotesdf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment