请原谅我,如果我不清楚,因为我是新的张贴在这里。
尝试使用以下代码从雅虎财经中提取数据和股票代码。当从yfinance中提取时,我想通过添加到一个名为“stocks_not_downloaded”的空列表中来跟踪每个未能下载的代码(无论是由于股票被摘牌还是其他原因)。为此,我使用了一个try / except块,这样如果一个股票下载失败,它就会附加到stocks_not_downloaded列表中。
当运行代码时,股票下载,当出现异常时,它会显示例如“1 Failed download:[“Ticker”]:异常(“% ticker%:未找到时区,符号可能已删除')”
但
A)它无法打印(“无法获取数据:“,股票代码),我已经设置了它这样做,并. B)当运行stocks_not_downloaded_list时,它返回“[]”-一个空列表
尽管输出了上述消息,但它似乎未能将错误识别为正常异常。我读到yfinance可能有一些奇怪的东西,但我不能弄清楚。
我将发布代码和输出。先谢了。
我正在使用这个数据集(https://onedrive.live.com/?authkey=%21ANM0er8uwPV%2DmRg&id=3B0309A579EBD636%2113144&cid=3B0309A579EBD636)-不确定它的草图是否链接到一个onedrive,但让我知道如果你想要它在另一种格式。
验证码
#Imports
import numpy as np
import pandas as pd
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
import datetime as dt
import mplfinance as mpf
import time
import os
from os import listdir
from os.path import isfile, join
import yfinance as yf
yf.pdr_override()
stocks_not_downloaded = []
missing_stocks = []
#Saves Stock Data to CSV
def save_to_csv_from_yahoo(folder, ticker, syear, smonth, sday, eyear, emonth, eday):
start = dt.datetime(syear, smonth, sday)
end = dt.datetime(eyear, emonth, eday)
try:
print("Get Data for :", ticker)
df = web.DataReader(ticker, start, end)["Adj Close"]
time.sleep(10)
df.to_csv(folder + ticker + ".csv")
except Exception as ex:
stocks_not_downloaded.append(ticker)
print("Could not Get Data for:", ticker)
#Returns a Named Column Data from CSV
def get_column_from_csv(file, col_name):
try:
df = pd.read_csv(file)
except FileNotFoundError:
print("File Does Not Exist")
else:
return df[col_name]
#Defines tickers and folder
tickers = get_column_from_csv("C:/Users/15189/Documents/Python_For_Finance/Wilshire-5000-Stocks.csv","Ticker")
folder = "/Users/15189/Documents/Python_For_Finance/Stock_List/"
for x in range(20):
save_to_csv_from_yahoo(folder, tickers[x], 2017, 1, 1, 2021, 8, 19)
print("Finished")
stocks_not_downloaded
你可以看到我正在使用:
try:
print("Get Data for :", ticker)
df = web.DataReader(ticker, start, end)["Adj Close"]
time.sleep(10)
df.to_csv(folder + ticker + ".csv")
except Exception as ex:
stocks_not_downloaded.append(ticker)
print("Could not Get Data for:", ticker)
但在跑步时:
for x in range(20):
save_to_csv_from_yahoo(folder, tickers[x], 2017, 1, 1, 2021, 8, 19)
print("Finished")
stocks_not_downloaded
我得到以下输出,结尾有一个“[]”--一个空的列表,列出了没有沿着下载的股票,没有“无法获取数据:“消息。我真的很感激你
输出:
Get Data for : A
[*********************100%%**********************] 1 of 1 completed
Get Data for : AA
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAL
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAME
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAN
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAOI
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAON
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAP
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAPL
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAT
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAWW
[*********************100%%**********************] 1 of 1 completed
1 Failed download:
['AAWW']: Exception('%ticker%: No timezone found, symbol may be delisted')
Get Data for : AAXN
[*********************100%%**********************] 1 of 1 completed
1 Failed download:
['AAXN']: Exception('%ticker%: No timezone found, symbol may be delisted')
Get Data for : ABBV
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABC
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABCB
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABEO
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABG
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABIO
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABM
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABMD
[*********************100%%**********************] 1 of 1 completed
Finished
[]
1条答案
按热度按时间prdp8dxp1#
不知道为什么错误没有被发现。我在当地试过,也遇到了同样的问题。似乎是pandas_datareader处理引发的错误的方式。
你可以做的一个选择是检查Adj Close是否为空:
看起来pandas-datareader也内置了一个yahoo reader。值得一试:https://pandas-datareader.readthedocs.io/en/latest/readers/yahoo.html