table = soup2.find('table', {'class': 'table'}) rankings = {} position = table.find('td', {'class': 'rankings-block__banner--pos'}) player = table.find('th',{'class': 'table-head__cell--player'}) team = table.find('th',{'class': 'table-head__cell--team'}) ranking = table.find('th',{'class': 'table-head__cell u-text-right'}) ranking[player] = {'position':position,'player': player, 'team': team,'ranking': ranking} for row in table.find_all('tr', {'class': 'table-body'}): position = row.find('td', {'class': 'table-body__cell table-body__cell--position u-text-right'}).text.strip() player = row.find_all('td', {'class': 'table-body__cell u-center-text'})[0].text.strip() team = row.find_all('td', {'class': 'table-body__cell u-center-text'})[1].text.strip() ranking = row.find('td', {'class': 'table-body__cell u-text-right'}).text.strip() rankings[player] = {'position': position, 'team': team, 'ranking': ranking} rankings
getting output IndexError: list index out of range
CodePudding user response:
first_data=soup.find("div",attrs={"data-cricket-scope":"odi"}).find("div",class_="rankings-block__top-player").get_text(strip=True,separator=" ").split(" ")
other_data=soup.find("div",attrs={"data-cricket-scope":"odi"}).find_all("tr",class_="table-body")
final_lst=[]
final_lst.append(first_data)
for i in data:
split_lst=i.get_text(strip=True,separator=" ").split(" ")
final_lst.append(split_lst)
Ouptut:
[['1', 'Babar', 'Azam', 'PAK', '873'],
['2', 'Virat', 'Kohli', 'IND', '844'],
['3', 'Rohit', 'Sharma', 'IND', '813'],
['4', 'Ross', 'Taylor', 'NZ', '801'],
['5', 'Aaron', 'Finch', 'AUS', '779'],
['6', 'Jonny', 'Bairstow', 'ENG', '775'],
['7', 'David', 'Warner', 'AUS', '762'],
['8', 'Shai', 'Hope', 'WI', '758'],
['9', 'Kane', 'Williamson', 'NZ', '754'],
['10', 'Quinton', 'de', 'Kock', 'SA', '747']]