Web Scraping NBA Games Python

I've been trying to write a code that extracts the names of the teams that play in that given day. This link (https://www.nba.com/scores) shows the games of the day. Looking through the source code, I noticed that the names are contained in those div tags with class="score-tile__team-name" Print Here. I wrote this code, but it returns blank. Thank you for the help!

from bs4 import BeautifulSoup
import requests

response = requests.get("https://www.nba.com/scores").text

soup = BeautifulSoup(response, 'html.parser')

for div in soup.find_all('div', class_="score-tile__team-name"):
    print(div.text)

Upvotes: 0

Views: 267

Answers (2)

chitown88
chitown88

Reputation: 28630

You acquire that data directly from the direct request.

import requests

jsonData = requests.get('https://data.nba.net/prod/v2/20200224/scoreboard.json').json()

for each in jsonData['games']:
    print ('%s @ %s' %(each['vTeam']['triCode'], each['hTeam']['triCode']))

Output:

MIA @ CLE
ATL @ PHI
MIL @ WAS
ORL @ BKN
NYK @ HOU
MIN @ DAL
PHX @ UTA
MEM @ LAC

Upvotes: 1

Robert Hafner
Robert Hafner

Reputation: 3895

You're using the "inspect element" tool, which shows you the page as it currently exists- and after a bunch of javascript is run. This is different than how the page is initially loaded. If you click view source you'll see the initial page that was loaded, before any javascript was run, which unfortunately does not contain the scores.

When you run request.get you're getting the initial page without the scores.

What you might want to do as a next step is open up the web tools, select Network, and then reload this page. Then look through what gets loaded to see if you can find the file that contains the scores.

Upvotes: 1

Related Questions