Я думаю, вы ищете что-то вроде этого:
import requests
from bs4 import BeautifulSoup
nba_players_url = 'https://www.nba.com/players'
nba_players_section = {'class': 'row nba-player-index__row'}
nba_info_section = {'class': 'nba-player-index__trending-item'}
nba_player_name_p = {'class': 'nba-player-index__name'}
r = requests.get(nba_players_url)
if r.ok:
soup = BeautifulSoup(r.content, 'html.parser')
players_section = soup.find('section', nba_players_section)
players = players_section.find_all('section', nba_info_section)
names = [
player.find('p', nba_player_name_p).get_text(separator=' ')
for player in players
]
print(names)
Но ваша жизнь будет намного проще с:
nba_players_url = 'https://www.nba.com/players/active_players.json'
Это даст вам JSON со всеми данными, которые вы ищете, поэтому нет необходимости анализировать с BeautifulSoup.
Проверьте это:
import requests
# just for printing players in a nice way
from pprint import pprint
nba_players_url = 'https://www.nba.com/players/active_players.json'
r = requests.get(nba_players_url)
if r.ok:
players = r.json()
pprint(players, width=40)
И так как мы находимся на это, вот другой вариант:
import pandas as pd
df = pd.read_json('https://www.nba.com/players/active_players.json')
И будет иметь что-то вроде этого:
>>> df[['firstName', 'lastName', 'isAllStar']]
firstName lastName isAllStar
0 Steven Adams False
1 Bam Adebayo True
2 LaMarcus Aldridge False
3 Nickeil Alexander-Walker False
4 Kyle Alexander False
.. ... ... ...
498 Thaddeus Young False
499 Trae Young True
500 Cody Zeller False
501 Ante Zizic False
502 Ivica Zubac False
[503 rows x 3 columns]
>>> df.info()
>>> <class 'pandas.core.frame.DataFrame'>
RangeIndex: 503 entries, 0 to 502
Data columns (total 14 columns):
firstName 503 non-null object
lastName 503 non-null object
jersey 503 non-null int64
pos 503 non-null object
posExpanded 503 non-null object
heightFeet 503 non-null object
heightInches 503 non-null object
weightPounds 503 non-null object
personId 503 non-null int64
teamData 503 non-null object
isAllStar 503 non-null bool
orderChar 503 non-null object
playerUrl 503 non-null object
displayName 503 non-null object
dtypes: bool(1), int64(2), object(11)
memory usage: 51.7+ KB
>>>