我想刮http://www.basketball-reference.com/awards/all_league.html一些分析,我的目標是一樣的東西下面轉換多列到單個基於另一列值蟒蛇
0 1日馬克 - 加索爾2014至2015年
1月1日安東尼 - 戴維斯2014- 2015
2第一詹姆斯2014-2015年
3個第一哈登2014-2015年
4第一庫裏2014-2015年
5第二保羅加索爾2014 - 2015年等
這是我迄今爲止的代碼,無論如何要做到這一點?任何建議/幫助非常感謝。
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')
if col:
year = col[0].find(text=True)
team = col[2].find(text=True)
player = col[3].find(text=True)
all_league_data.loc[len(all_league_data)] = [team, player, year]
all_league_data