"Yahoo" 不符合表格

2024-05-05 02:38:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从雅虎获取标准普尔500指数的数据。 安装了修复雅虎财务已经,并使用yf.下载()

我试着运行代码,它给出了以下输出

值错误:时间数据“yahoo”与格式“%Y-%m-%d”不匹配

代码如下所示:

import bs4 as bs
import datetime as dt
from datetime import datetime
import os 
import pandas as pd
import fix_yahoo_finance as yf
from pandas_datareader import data as web
import fix_yahoo_finance
import pickle 
import requests
def save_sp500_tickers():
    resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
    soup = bs.BeautifulSoup(resp.text, 'lxml')
    table = soup.find('table', {'class':'wikitable sortable'})
    tickers = []
    for row in table.findAll('tr')[1:]:
        ticker = row.findAll('td')[0].text
        tickers.append(ticker)

    with open('sp500tickers.pickle','wb') as f:
              pickle.dump(tickers, f)
    print(tickers)  

    return tickers


#save_sp500_tickers()


def get_data_from_yahoo(reload_sp500=False):
    if reload_sp500:
        tickers = save_sp500_tickers()
    else:
        with open('sp500tickers.pickle','rb') as f:
            tickers = pickle.load(f)

    if not os.path.exists('stock_dfs'):
        os.makedirs('stock_dfs')



    for ticker in tickers:
        print(ticker)
        if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
           start = dt.datetime(2000,1,1)
           end = dt.datetime(2016,12,31)
            df = yf.download(ticker, 'yahoo', start, end)
            df.to_csv('stock_dfs/{}.csv'.format(ticker))
        else:
            print('Already have{}'.format(ticker))

get_data_from_yahoo()

我是个初学者(已经探索python一个月了),所以解决方案可能很简单,但我找不到真正能回答我问题的东西。在

谢谢你!在


Tags: fromimportdatadatetimeosasstockdt