Now You Code 1: NBA Stat Leaderboards

Get the current leader of each stat provided by http://www.espn.com/nba/statistics and print the stat title and the list of player in order.

ex:

Offensive Leaders:
POINTS                                            PPG
1. Russell Westbrook, OKC                         31.4
2. Anthony Davis, NO                              28.6
3. DeMarcus Cousins, SAC                          28.5
4. James Harden, HOU                              27.9
5. Isaiah Thomas, BOS                             27.7
-------------------------------------------------------
ASSISTS                                           APG
1. James Harden, HOU                              11.9
2. Russell Westbrook, OKC                         10.3
3. John Wall, WSH                                 10.2
4. Chris Paul, LAC                                9.6
5. LeBron James, CLE                              8.4
-------------------------------------------------------
...

Suggested approach:

  • Use requests to download the page html
  • Use BeautifulSoup to extract the data
  • Create a function to process each stat box.
  • Loop through each stat box and process the stat
  • Use string format functions to format the output

In [ ]:
# Todo
1. Download the HTML
2. Find stat boxes with BeautifulSoup
3. Get Stat title, and list of players from each stat
4. Print results with formatting

In [18]:
import requests
from bs4 import BeautifulSoup

html = requests.get('http://www.espn.com/nba/statistics').text
soup = BeautifulSoup(html, 'lxml')
boxes = soup.select('#my-players-table .span-2 .mod-container')
for box in boxes:
    for row in box.select('table tr'):
        items = row.find_all('td')
        if len(items) > 2:
            items = items[1:]
        if len(items) > 1:
            print('{:50}{}'.format(items[0].get_text(), items[1].get_text()))
    print('-' * 55)


POINTS                                            PPG
1. Russell Westbrook, OKC                         31.4
2. Anthony Davis, NO                              28.6
3. DeMarcus Cousins, SAC                          28.5
4. James Harden, HOU                              27.9
5. Isaiah Thomas, BOS                             27.7
-------------------------------------------------------
ASSISTS                                           APG
1. James Harden, HOU                              11.9
2. Russell Westbrook, OKC                         10.3
3. John Wall, WSH                                 10.2
4. Chris Paul, LAC                                9.6
5. LeBron James, CLE                              8.4
-------------------------------------------------------
FIELD GOAL %                                      FG%
1. DeAndre Jordan, LAC                            .677
2. Rudy Gobert, UTAH                              .665
3. Clint Capela, HOU                              .640
4. Dwight Howard, ATL                             .639
5. Steven Adams, OKC                              .595
-------------------------------------------------------
REBOUNDS                                          RPG
1. Hassan Whiteside, MIA                          14.3
2. Andre Drummond, DET                            13.4
3. DeAndre Jordan, LAC                            13.4
4. Dwight Howard, ATL                             13.1
5. Rudy Gobert, UTAH                              12.2
-------------------------------------------------------
BLOCKS                                            BLKPG
1. Rudy Gobert, UTAH                              2.57
2. Anthony Davis, NO                              2.54
3. Myles Turner, IND                              2.47
4. Joel Embiid, PHI                               2.38
5. Hassan Whiteside, MIA                          2.24
-------------------------------------------------------
STEALS                                            STPG
1. John Wall, WSH                                 2.30
2. Chris Paul, LAC                                2.25
3. Draymond Green, GS                             2.11
4. Trevor Ariza, HOU                              2.08
5. Giannis Antetokounmpo, MIL                     1.91
-------------------------------------------------------

In [ ]: