S& P 500列表python脚本崩溃

时间:2017-08-25 21:57:18

标签: python finance

所以我一直在关注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数据读取器。

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\4.8.5.35155\bin\Firefox_Extension\{442718d9-475e-452a-b3e1-fb1ee16b8e9f}\components;C:\Users\Mehdi\AppData\Roaming\Dashlane\4.8.5.35155\ucrt]

1 个答案:

答案 0 :(得分:0)

您的代码中有两个错误:

  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))更改为{ {1}})