Reputation: 3
I want to return only the names of these 3 players (in the url).. The current code returns their name, their team, and their basketball association. Is there something I can specify in the code to return only names?
Data scraping from here:
import requests
from bs4 import BeautifulSoup
def bball_spider(str):
source_code = requests.get(str)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
# Players
for elements in soup.find('table' , {'id' : 'stats'}).findAll('a'):
names = elements.string
print(names)
str = input("Enter the Query Result URL ")
bball_spider(str)
Upvotes: 0
Views: 195
Reputation: 6518
You are almost there, but first let me mention this since it seems you are new to Python: You shouldn't name a variable str
, because it shadows the built-in str class, so that's something I modified in the code shown below. The important modification is that I changed your .findAll('a')
to .findAll('td',{'class':'left active'})
, inspecting the element we can see that all the names of the players are in a <td>
tag with class left active
. I also changed the iterating var to element
instead of its plural, so it makes more sense semantically speaking. Also do note the code you posted is not correctly idented, but I think this was just a formatting issue when you pasted it here.
import requests
from bs4 import BeautifulSoup
def bball_spider(url):
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
# Players
for element in soup.find('table',{'id' : 'stats'}).findAll('td',{'class':'left active'}):
names = element.string
print(names)
url = '''https://www.basketball-reference.com/play-index/psl_finder.cgi?request=1&match=single&type=totals&per_minute_base=36&per_poss_base=100&season_start=1&season_end=-1&lg_id=NBA&age_min=0&age_max=99&is_playoffs=N&height_min=0&height_max=99&year_min=2017&year_max=2017&birth_country_is=Y&as_comp=gt&as_val=0&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=fg3_pct&c1comp=gt&c1val=40&c2stat=fg3a&c2comp=gt&c2val=164&c3stat=dbpm&c3comp=gt&c3val=0&order_by=ws'''
bball_spider(url)
This will print:
Chris Paul
Otto Porter
Joe Ingles
Upvotes: 1