Ryan T. Donnelly
Ryan T. Donnelly

Reputation: 153

How to obtain a JSON response from the stats.nba.com API?

I'm just trying to simply use a Python get request to access JSON data from stats.nba.com. It seems pretty straight-forward as I can enter the URL into your browser and get the results I'm looking for. However, whenever I run this the program just runs to no end. I'm wondering if I have to include some type of headers information in my get request.

The code is below:

import requests

url = 'http://stats.nba.com/stats/commonteamroster?LeagueID=00&Season=2017-18&TeamID=1610612756'
response=requests.get(url)
print response.text

Upvotes: 1

Views: 6304

Answers (3)

Cross
Cross

Reputation: 796

the minimum header for python request, as I tested, is:

request_headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
    'Referer': 'https://www.nba.com/',
    # 'Accept': 'application/json, text/plain, */*',
    # 'Accept-Encoding': 'gzip, deflate, br',
    # 'Host': 'stats.nba.com',
    # 'Accept-Language': 'en-US,en;q=0.9',
    # 'Connection': 'keep-alive'
}

then, run it:

response = requests.get(url, headers = request_headers)

Upvotes: 0

Ballack
Ballack

Reputation: 966

I have tried to visit the url you given, you can add header to your request to avoid this problem (the minimum information you need to provide is User-Agent, I think you can use more header information as you can):

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
response = requests.get(url, headers=headers)

The stats.nba.com website need your 'User-Agent' header information.

You can get your request header information from Network tab in the browser.

Take chrome as example, when you press F12, and visit url you given, you can find the relative request information, the most useful information is request headers.

enter image description here

Upvotes: 5

user4066647
user4066647

Reputation:

You need to use headers. Try copying from your browser's network tab. Here's what worked for me:

request_headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'en-US,en;q=0.8',
    'Connection': 'keep-alive',
    'Host': 'stats.nba.com',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
}

And here's the modified get:

response = requests.get(url, headers = request_headers)

Upvotes: 3

Related Questions