1

My code so far, works for different table on FBref website, however struggling to get player details. The below code:

import requests
from bs4 import BeautifulSoup, Comment


url = 'https://fbref.com/en/squads/18bb7c10/Arsenal-Stats'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

table = BeautifulSoup(soup.select_one('#stats_standard').find_next(text=lambda x: isinstance(x, Comment)), 'html.parser')

#print some information from the table to screen:
for tr in table.select('tr:has(td)'):
tds = [td.get_text(strip=True) for td in tr.select('td')]
print('{:<30}{:<20}{:<10}'.format(tds[0], tds[3], tds[5]))

Gives me the error:

AttributeError: 'NoneType' object has no attribute 'find_next'

2
  • 3
    You're getting this error as you are getting NoneType object from select_one. This is because '#stats_standard' can't be found, to be selected. Commented Feb 8, 2021 at 10:51
  • Please do not vandalize your post by removing the code (rolled back). Commented Apr 25, 2021 at 12:27

1 Answer 1

1

What happens?

As mentioned, there is no table with id stats_standard the id should be stats_standard_10728

How to fix and go a bit generic

Change your table selector to:

table = soup.select_one('table[id^="stats_standard"]')

Example

import requests
from bs4 import BeautifulSoup, Comment


url = 'https://fbref.com/en/squads/18bb7c10/Arsenal-Stats'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

table = soup.select_one('table[id^="stats_standard"]')

#print some information from the table to screen:
for tr in table.select('tr:has(td)'):
    tds = [td.get_text(strip=True) for td in tr.select('td')]
    print('{:<30}{:<20}{:<10}'.format(tds[0], tds[3], tds[5]))

Just in case

You can make your life much easier using pandas read_html() to grab, display and modify table data.

Example

import pandas as pd
pd.read_html('https://fbref.com/en/squads/18bb7c10/Arsenal-Stats')[0]

Need to know, how to handle commented tables? Check -> How to scrape table from fbref could not be found by BeautifulSoup?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.