You can use find_all
to access the data from the statsListBlock
div
s:
import requests, re
from bs4 import BeautifulSoup as soup
d = soup(requests.get('https://www.premierleague.com/players/10483/Rolando-Aarons/stats').text, 'html.parser')
new_d = d.find_all('div', {'class':'statsListBlock'})
results = {i.div.text[1:-1]:{c.span.contents[0]:c.span.contents[-2].text for c in i.find_all('div', {'class':'normalStat'})} for i in new_d}
new_results = {a:{re.sub('\s+$', '', c):re.findall('\d+', d)[0] for c, d in b.items()} for a, b in results.items()}
Output:
{'Attack': {'Goals': '2', 'Goals per match': '0', 'Headed goals': '1', 'Goals with right foot': '0', 'Goals with left foot': '1', 'Penalties scored': '0', 'Freekicks scored': '0', 'Shots': '11', 'Shots on target': '3', 'Shooting accuracy %': '27', 'Hit woodwork': '1', 'Big chances missed': '0'}, 'Team Play': {'Assists': '1', 'Passes': '197', 'Passes per match': '10', 'Big chances created': '1', 'Crosses': '19', 'Cross accuracy %': '21', 'Through balls': '0', 'Accurate long balls': '8'}, 'Discipline': {'Yellow cards': '2', 'Red cards': '0', 'Fouls': '11', 'Offsides': '1'}, 'Defence': {'Tackles': '20', 'Tackle success %': '70', 'Blocked shots': '2', 'Interceptions': '8', 'Clearances': '11', 'Headed Clearance': '6', 'Recoveries': '43', 'Duels won': '54', 'Duels lost': '67', 'Successful 50/50s': '14', 'Aerial battles won': '7', 'Aerial battles lost': '12', 'Errors leading to goal': '1'}}
To associate the name:
new_result = {d.find('div', {'class':'name t-colour'}).text:new_results}
Output:
{'Rolando Aarons': {'Attack': {'Goals': '2', 'Goals per match': '0', 'Headed goals': '1', 'Goals with right foot': '0', 'Goals with left foot': '1', 'Penalties scored': '0', 'Freekicks scored': '0', 'Shots': '11', 'Shots on target': '3', 'Shooting accuracy %': '27', 'Hit woodwork': '1', 'Big chances missed': '0'}, 'Team Play': {'Assists': '1', 'Passes': '197', 'Passes per match': '10', 'Big chances created': '1', 'Crosses': '19', 'Cross accuracy %': '21', 'Through balls': '0', 'Accurate long balls': '8'}, 'Discipline': {'Yellow cards': '2', 'Red cards': '0', 'Fouls': '11', 'Offsides': '1'}, 'Defence': {'Tackles': '20', 'Tackle success %': '70', 'Blocked shots': '2', 'Interceptions': '8', 'Clearances': '11', 'Headed Clearance': '6', 'Recoveries': '43', 'Duels won': '54', 'Duels lost': '67', 'Successful 50/50s': '14', 'Aerial battles won': '7', 'Aerial battles lost': '12', 'Errors leading to goal': '1'}}}