import requests
from bs4 import BeautifulSoup
page = requests.get('https://www.nba.com/players')
soup = BeautifulSoup(page.text, 'html.parser')
players = soup.find(class_ ='row nba-player-index__row')
players_info = players.find_all(class_='nba-player-index__trending-item small-4 medium-3 large-2 team-okc-thunder')
players_name = [players_info.find(class_ ='nba-player-index__details').get_text() for player in players_info]
print(players_name)
这是我的错误
C:\Users\moham\PycharmProjects\WebScrape\Nba\venv\Scripts\python.exe C:/Users/moham/PycharmProjects/WebScrape/Nba/Nba.py
Traceback (most recent call last):
File "C:/Users/moham/PycharmProjects/WebScrape/Nba/Nba.py", line 10, in <module>
players_name = [players_info.find(class_ ='nba-player-index__details').get_text() for player in players_info]
File "C:/Users/moham/PycharmProjects/WebScrape/Nba/Nba.py", line 10, in <listcomp>
players_name = [players_info.find(class_ ='nba-player-index__details').get_text() for player in players_info]
File "C:\Users\moham\PycharmProjects\WebScrape\Nba\venv\lib\site-packages\bs4\element.py", line 2080, in __getattr__
raise AttributeError(
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
Process finished with exit code 1
它总是说调用
'find_all() when you meant to call find() and attributes'
,我试着把它改成find(),但还是不行。我尝试了所有的解决方案,但仍然不起作用,希望有人能帮助我,谢谢大家
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')