Python中用于股票代码的用户输入消毒

问题描述 投票:0回答:1

我想从用户那里获得用户输入,这是股票代码。我想改进代码,以便如果有人输入的字符串不是股票行情代码或不存在的行情代码,则不会出错]


import datetime #library to get time, including the current time
import pandas_datareader.data as web
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
from matplotlib import style
import matplotlib as mpl
import matplotlib.collections as collections
import numpy as np


#print(plt.style.available)
# check available stye
plt.style.use('seaborn-dark-palette')

stockCode = input("Enter stock ticker code: ")


startDate = datetime.datetime(2010,1,1)
endDate = datetime.date.today()


df = web.DataReader(stockCode, 'yahoo', startDate, endDate)
df.tail(-5)

您能给我一些建议吗?谢谢

python yahoo-finance
1个回答
0
投票

您可以使用名为yahooquery的python软件包。在获取股票行情的历史价格(您也可以使用该软件包)之前,请执行以下操作:

Ticker(‘<ticker>’).quote_type

如果未找到代码,您将按照以下方式获取字典:

<ticker>: ‘Quote not found for ticker symbol: <ticker>’

否则,请继续获取历史价格:

Ticker(‘<ticker>’).history(start=2010-01-01)

默认结束日期是今天。

© www.soinside.com 2019 - 2024. All rights reserved.