所以我用这个在 Python for Finance 上,它总是给我错误——
1) line 22, in <module> save_sp500_tickers() and
2) line 8, in save_sp500_tickers
soup = bs.BeautifulSoup(resp.text,'lxml')and
3) line 165, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml.
Do you need to install a parser library?
我已经为此工作了一整天,老实说我拒绝放弃,对此的任何帮助将不胜感激。另外,如果有人对pickle以外的东西有任何建议,并且可以帮助写一些东西,让我可以在没有pickle的情况下调用SP500,那就太好了。
import bs4 as bs
import pickle
import requests
import lxml
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()
按原样运行您的代码可以在我的系统上运行。也许,正如 Eric 建议的那样,您应该安装 lxml。
不幸的是,如果您使用的是 Windowspip install lxml
除非您设置了完整的编译器基础结构,否则它不起作用,而您可能没有。
幸运的是,您可以从以下位置获取预编译的二进制安装程序http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml- 确保您选择的 Python 版本与您的 Python 版本相匹配,以及它是 32 位还是 64 位。
Edit:只是为了兴趣,尝试改变线路
soup = bs.BeautifulSoup(resp.text, 'html.parser') # use Python's built-in parser instead
See https://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser https://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser获取可用解析器的列表。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)