【发布时间】:2018-06-23 13:46:06
【问题描述】:
使用 Python,我正在尝试从 U.S. Today Money Stocks Under $10 中删除 10 美元以下的股票表。然后将每个元素添加到一个列表中(这样我就可以遍历每个股票)。目前,我有这个代码:
resp = requests.get('https://money.usnews.com/investing/stocks/stocks-under-10')
soup = bs.BeautifulSoup(resp.text, "lxml")
table = soup.find('table', {'class': 'table stock full-row search-content'})
tickers = []
for row in table.findAll('tr')[1:]:
ticker = str(row.findAll('td')[0].text)
tickers.append(ticker)
我不断收到错误:
Traceback (most recent call last):
File "sandp.py", line 98, in <module>
sandp(0)
File "sandp.py", line 40, in sandp
for row in table.findAll('tr')[1:]:
AttributeError: 'NoneType' object has no attribute 'findAll'
【问题讨论】:
-
你能告诉我们
table的样子吗?只是为了确保您确实得到了结果。 -
@TomasFarias 我添加了
print table行,终端显示none。 -
好吧,
soup.find('table', {'class': 'table stock full-row search-content'})似乎找不到结果。你确定那是桌子的正确类别吗?您是否检查过 soup 是否真的访问了正确的内容?也许您必须将一些标头传递给requests.get。 -
@TomasFarias 是的,它是正确的表
标签: python web-scraping beautifulsoup stocks