python和网页抓取的新手段。我试图抓取http://www.basketball-reference.com/awards/all_league.html进行一些分析,只得到了目前为止。使用下面的代码,我可以在指定年份的时候只擦除3行并获取“列表索引超出范围”的错误。任何帮助/提示表示赞赏。Python 2.7网页抓取 - 列表索引超出范围
r = requests.get('http://www.basketball-reference.com/awards/all_league.html')
soup=BeautifulSoup(r.text.replace(' ','').replace('>','').encode('ascii','ignore'),"html.parser")
all_league_data = pd.DataFrame(columns = ['year','team','player'])
stw_list = soup.findAll('div', attrs={'class': 'stw'}) # Find all 'stw's'
for stw in stw_list:
table = stw.find('table', attrs = {'class':'no_highlight stats_table'})
for row in table.findAll('tr'):
col = row.findAll('td')
year = col[0].find(text=True)
print year