0

I have followed a video and an article(while trying to learn BS4). Both scrape similarly and I keep getting this error either way:

headers = [th.getText() for th in soup.findAll('tr', limit=2)[0].findALl('th')] TypeError: 'NoneType' object is not callable

from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd

url = "https://www.basketball-reference.com/leagues/NBA_2022_per_game.html"
html = urlopen(url)
soup = BeautifulSoup(html, features="lxml")

headers = [th.getText() for th in soup.findAll('tr', limit=2)[0].findALl('th')]
AJG
  • 3
  • 1

1 Answers1

0

Ultimately you have a typo with .findALl() when you wanted to use .findAll(). However, I like to use .find_all() instead.

Read about it here

from bs4 import BeautifulSoup
from urllib.request import urlopen    

url = "https://www.basketball-reference.com/leagues/NBA_2022_per_game.html"
html = urlopen(url)
soup = BeautifulSoup(html, features="lxml")

headers = [th.getText() for th in soup.findAll('tr', limit=2)[0].findAll('th')]

# Could use .find_all()
# headers = [th.getText() for th in soup.find_all('tr', limit=2)[0].find_all('th')]

Output:

print(headers)
['Rk', 'Player', 'Pos', 'Age', 'Tm', 'G', 'GS', 'MP', 'FG', 'FGA', 'FG%', '3P', '3PA', '3P%', '2P', '2PA', '2P%', 'eFG%', 'FT', 'FTA', 'FT%', 'ORB', 'DRB', 'TRB', 'AST', 'STL', 'BLK', 'TOV', 'PF', 'PTS']
chitown88
  • 27,527
  • 4
  • 30
  • 59
  • Thanks! Yeah, I was using requests the first go but switched it up when trying to solve the error. – AJG Jan 27 '22 at 15:48