我有以下代码块,当使用Python3在我的Ubuntu计算机上执行时会遇到酸洗的递归错误。我不明白为什么要被腌制的对象不是特别复杂并且不涉及任何自定义对象。事实上,它只是大约500个元素的列表(大约);列表中的每个元素都只是一个字符串。在我看来,我应该能够毫无问题地序列化这个对象。为什么我遇到递归限制错误?我知道我可以使用import sys
和sys.setrecursionlimit()
来提升递归限制但我坦率地感到惊讶,我必须为这样一个微不足道的对象做到这一点。
from urllib import request
from bs4 import BeautifulSoup
import pickle
def get_standard_and_poors_500_constituents():
# URL request, URL opener, read content.
req = request.Request(
"http://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
)
opener = request.urlopen(req)
# Convert bytes to UTF-8.
content = opener.read().decode()
soup = BeautifulSoup(content, "lxml")
# HTML table we actually need is the first.
tables = soup.find_all("table")
external_class = tables[0].findAll("a", {"class":"external text"})
c = [ext.string for ext in external_class if not "reports" in ext]
return c
sp500_constituents = get_standard_and_poors_500_constituents()
spdr_etf = "SPY"
sp500_index = "^GSPC"
def main():
import datetime as dt
today = dt.datetime.today().date()
fname = "sp500_constituents_" + str(today) + ".pkl"
with open(fname, "wb") as f:
pickle.dump(sp500_constituents, f)
if __name__ == "__main__":
main()