我正在为学校(A级)的项目编写代码,需要能够下载库存数据并绘制图表。我能够使用matplotlib绘制数据图表。但是,我只能使用一定数量的库。
我需要在不导入库的情况下获取数据,但是无法做到。我曾尝试从 https://query1.finance.yahoo.com/v7/finance/download/ticker 下载,但面包屑值不断变化,因此我不断从错误的cookie中获取错误。
我该如何解决?还是有一个更简单的数据存储网站?
我的代码:
import requests
r = requests.get("query1.finance.yahoo.com/v7/finance/download/…)
file = open(r"MSFT.csv", 'w')
file.write(r.text) file.close()
答案 0 :(得分:0)
从https://datahub.io下载数据,否则您可以订阅来自不同交易所的第三方供应商的实时数据提要。
答案 1 :(得分:0)
您说:“我只允许使用一定数量的库。”这意味着什么?您应该能够使用需要使用的所有库。运行以下脚本。它将从Yahoo下载股票数据并绘制时间序列。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.optimize as sco
import datetime as dt
import math
from datetime import datetime, timedelta
from pandas_datareader import data as wb
from sklearn.cluster import KMeans
np.random.seed(777)
start = '2019-4-30'
end = '2019-10-31'
# N = 90
# start = datetime.now() - timedelta(days=N)
# end = dt.datetime.today()
tickers = ['MMM',
'ABT',
'ABBV',
'ABMD',
'AAPL',
'XEL',
'XRX',
'XLNX',
'XYL',
'YUM',
'ZBH',
'ZION',
'ZTS']
thelen = len(tickers)
price_data = []
for ticker in tickers:
prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Adj Close']]
price_data.append(prices.assign(ticker=ticker)[['ticker', 'Adj Close']])
df = pd.concat(price_data)
df.dtypes
df.head()
df.shape
pd.set_option('display.max_columns', 500)
df = df.reset_index()
df = df.set_index('Date')
table = df.pivot(columns='ticker')
# By specifying col[1] in below list comprehension
# You can select the stock names under multi-level column
table.columns = [col[1] for col in table.columns]
table.head()
plt.figure(figsize=(14, 7))
for c in table.columns.values:
plt.plot(table.index, table[c], lw=3, alpha=0.8,label=c)
plt.legend(loc='upper left', fontsize=12)
plt.ylabel('price in $')