2

I'm trying to learn web scraping, and was trying to scrape the NBA website. I've been trying to get the details of the players and eventually would like to export them into a CSV file. Currently when I print out the values of the list individually, i am able to get the output i am looking for. However when i attempt to print the whole list, it appears as unreadable object code.

from selenium import webdriver
from bs4 import BeautifulSoup
import csv


class Player():
    def __init__(self):
        self.name = ""
        self.link = ""
        self.Weight = ""
        self.Height = ""


driver = webdriver.PhantomJS(executable_path=r'C:\Users\mrtho\Google Drive\Scraping\phantomjs.exe')

url = 'https://www.nba.com/players'

driver.get(url)

soup = BeautifulSoup(driver.page_source,'lxml')

div = soup.find('div',class_='small-12 columns')

player_list = []

for a in div.find_all('a'):
    for name in a.find_all('span',class_='name-label'):

        new_play = Player()
        new_play.name = name.text
        new_play.link = 'https://www.nba.com'+a['href']
        player_list.append(new_play)


driver.quit()

for p in player_list[0:2]:


    driver = webdriver.PhantomJS(executable_path=r'C:\Users\mrtho\Google Drive\Scraping\phantomjs.exe')

    url = p.link

    driver.get(url)

    soup = BeautifulSoup(driver.page_source,'lxml')


    height1 = soup.find('p',class_='nba-player-vitals__top-info-metric')


    weight1 = soup.find('section',class_='nba-player-vitals__top-right small-6')
    weight2 = weight1.find('p',class_='nba-player-vitals__top-info-metric')
    # print('Weight: '+weight2.text)

    p.Height = height1.text
    p.Weight = weight2.text

    driver.quit()


for p in player_list[0:2]:
    print ('\n')
    print (p.name)
    print (p.link)
    print (p.Height)
    print (p.Weight)
    print ('\n')

print(player_list, sep = "\n")

When printing for p in player_list[0,2] I am able to obtain the details of the players, eg:

Adams, Steven
https://www.nba.com/players/steven/adams/203500
2.13m
120.2kg

However, when I print the whole player list, my output appears as

[<__main__.Player object at 0x0000021F53AF3278>, <__main__.Player object at 0x0000021F53AF32B0>, <__main__.Player object at 0x0000021F53AF32E8>,.....
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • The duplicate target explains how to create a `__str__` method to pretty-print your class instances. To print the list itself you'd need to do something like `print([str(player) for player in list_of_players])` . Similar https://stackoverflow.com/a/54135937/5320906 – snakecharmerb Sep 18 '19 at 15:59
  • @snakecharmerb `print(list_of_players)` will call `str()` on each element automatically. There is no need for the list comprehension. – Code-Apprentice Sep 18 '19 at 16:03
  • @Code-Apprentice It doesn't in my (default 3.7) repl.... – snakecharmerb Sep 18 '19 at 16:07
  • @snakecharmerb I stand corrected. `print()` calls `repr()` on each element: https://repl.it/@codeguru/BestGiantSymbols. – Code-Apprentice Sep 18 '19 at 16:16

0 Answers0