Created
May 4, 2018 10:15
-
-
Save canwe/3e17218a701675be2442cfcad31c58ec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
""" | |
get DJI data | |
@author: Dazhuang | |
""" | |
import json | |
import re | |
import requests | |
def retrieve_dji_list(): | |
r = requests.get('http://money.cnn.com/data/dow30/') | |
search_pattern = re.compile('class="wsod_symbol">(.*?)<\/a>.*<span.*">(.*?)<\/span>.*\n.*class="wsod_stream">(.*?)<\/span>') | |
dji_list_in_text = re.findall(search_pattern, r.text) | |
dji_list = [] | |
for item in dji_list_in_text: | |
dji_list.append({'code': item[0], 'name': item[1], 'price': float(item[2])}) | |
return dji_list | |
def retrieve_quotes_historical(stock_code, start = '', end = ''): | |
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment