标准普尔 500 指数 python 脚本崩溃

S&P 500 List python script crashes

所以我一直在关注有关 Python 金融的 youtube 教程,由于雅虎现在对金融市场关闭了大门,因此造成了一些居住问题。

我运行这个代码

    import bs4 as bs
import datetime as dt 
import os
import pandas as pd
import pandas_datareader.data as web
import pickle
import requests
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf


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(ticker, 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')

    start = dt.datetime(2000, 1, 1)
    end = dt.datetime(2017, 8, 24)

    for ticker in tickers:
        if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
            data = pdr.get_data_yahoo(ticker, start, end)
            df.to_csv('stock_dfs/{}.csv'.format(ticker))
        else:
            print('Already have {}'.format(ticker))

    get_data_from_yahoo()

它因一些错误而崩溃,而不仅仅是一个。第一个错误是我应该覆盖 pandas 数据 reader。

DeprecationWarning: 
Auto-overriding of pandas_datareader's get_data_yahoo() is deprecated and will be removed in future versions.
Use pdr_override() to explicitly override it.
DeprecationWarning)  

如何覆盖它?我真的不知道该怎么做,我是 Python 的新手 - 抱歉我是个菜鸟。

然后我们有这个:

    get_data_from_yahoo()
  File "C:\Users\Mehdi\Desktop\Python finance\SP500_List.py", line 36, in get_data_from_yahoo
    tickers = pickle.load(f)

我真的不明白为什么会这样,因为我已经和Youtuber核对过我的代码,他们是匹配的。所以一些指针会被应用。

最后,我有这个错误:

  EOFError: Ran out of input

我也不知道是什么意思

除此之外,我已经安装了 'fix_yahoo_finance' 软件包并尝试使用新代码进行安装,但仍然无法正常工作。

欢迎提供任何帮助。谢谢:)

完整错误列表:

C:\Users\Mehdi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\fix_yahoo_finance\__init__.py:43: DeprecationWarning: 
    Auto-overriding of pandas_datareader's get_data_yahoo() is deprecated and will be removed in future versions.
    Use pdr_override() to explicitly override it.
  DeprecationWarning)
Traceback (most recent call last):
  File "C:\Users\Mehdi\Desktop\Python finance\SP500_List.py", line 51, in <module>
    get_data_from_yahoo()
  File "C:\Users\Mehdi\Desktop\Python finance\SP500_List.py", line 36, in get_data_from_yahoo
    tickers = pickle.load(f)
EOFError: Ran out of input
[Finished in 3.1s with exit code 1]
[shell_cmd: python -u "C:\Users\Mehdi\Desktop\Python finance\SP500_List.py"]
[dir: C:\Users\Mehdi\Desktop\Python finance]
[path: C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Users\Mehdi\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\Mehdi\AppData\Local\Programs\Python\Python36-32\;C:\Users\Mehdi\AppData\Local\Microsoft\WindowsApps;C:\Python36\Scripts;C:\Users\Mehdi\AppData\Roaming\Dashlane.8.5.35155\bin\Firefox_Extension\{442718d9-475e-452a-b3e1-fb1ee16b8e9f}\components;C:\Users\Mehdi\AppData\Roaming\Dashlane.8.5.35155\ucrt]

你的代码有两个错误:

  1. 在函数save_sp500_tickers()的第22行,而不是这个:

    with open("sp500tickers.pickle", "wb") as f:
        pickle.dump(ticker, f)
    

    应该是:

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

    所以它是代码而不是代码

  2. 在函数get_data_from_yahoo()的第47行,而不是这个:

    if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
        data = pdr.get_data_yahoo(ticker, start, end)
        df.to_csv('stock_dfs/{}.csv'.format(ticker))
    

    应该是:

    if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
        data = pdr.get_data_yahoo(ticker, start, end)
        data.to_csv('stock_dfs/{}.csv'.format(ticker))
    

    您需要使用 data 而不是 df(在视频中使用了 df = web.DataReader(ticker, 'yahoo', start, end),您在 data = pdr.get_data_yahoo(ticker, start, end) 中进行了更改,但您忘记更改 df.to_csv('stock_dfs/{}.csv'.format(ticker))data.to_csv('stock_dfs/{}.csv'.format(ticker)))