import re
import urllib.request
from bs4 import BeautifulSoup as bs
site = ‘https://en.wikipedia.org/wiki/List_of_S%26P_500_companies’
page = urllib.request.urlopen(site)
soup = bs(page.read())
print(soup.prettify())
table = soup.find(‘table’,{‘class’:‘wikitalbe’})
SD = dict()
for row in table.findall(‘tr’):
col = row.findall(‘td’)
if len(col)>0:
ticker = str(col[0].string.strip())
sector = str(col[3].string.strip()).lower()
SD[ticker] = sector
执行之后出现报错:
AttributeError: ‘NoneType’ object has no attribute ‘findall’
请教大神!