|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
示例代码里给的是yahoo fiance 网站,运行后url报错,是不是需要翻墙?
原教程地址:http://blog.csdn.net/wizardforcel/article/details/54407212 【第九章】
代码:(请大佬帮个忙,大一小白有点懵......如果这个网站数据搞不到,下面的教程就很难进行下去了 )
- import matplotlib.pyplot as plt
- import numpy as np
- import urllib
- import matplotlib.dates as mdates
- def bytespdate2num(fmt,encoding = 'utf-8'):
- strconverter = mdates.strpdate2num(fmt)
- def bytesconverter(b):
- s=b.decode(encoding)
- return strconverter(s)
- return bytesconverter
- def graph_data(stock):
- stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0'+stock+'/chatdata;type=quote;range=10y/csv'
- source_code = urllib.request.urlopen(stock_price_url).read().decode()
- stock_data=[]
- split_source = source_code.split('\n')
- for line in split_source:
- split_line = line.split(',')
- if len(split_line)==6:
- if'values' not in line:
- stock_data.append(line)
-
- #使用numpy中的loadTXT函数,并将这六个元素解构到六个变量
- date,closep,highp,lowp,openp,volue = np.loadtxt(stock_data,
- delimiter = ',',
- unpack =True,
- #%Y = full year.2015
- #%y = aprtial year 2015
- #%m = number month
- #%d = number day
- #%H = hours
- #%M = minutes
- #%S = secounds
- # 12-06-2014
- # %m-%d-%Y
- converters = {0:bytespdate2num('%Y%m%d')})
- plt.plot_date(date,closep,'-',label = 'price')
- plt.xlabel('Date')
- plt.ylabel('Price')
- plt.title('Check it out')
- plt.legend()
- plt.show()
- graph_data('TSLA')
复制代码
请大佬能帮我看看
|
|