TAGS :Viewed: 6 - Published at: a few seconds ago

[ Python Requests Client Error: Bad Request, but works after website has been opened in browser ]

I'm having a weird problem where I get a 400 Client Error: Bad Request on a url that I have never opened, but then if I open that same url with my browser and then close it, I am all of a sudden able to access it with Requests.

Here is my code:

import requests
url = 'http://stats.nba.com/stats/boxscore?GameID=0021500669&RangeType=0&StartPeriod=0&EndPeriod=0&StartRange=0&EndRange=0'
response = requests.get(url)
response.raise_for_status() # raise exception if invalid response
data = response.json()['resultSets'][0]['rowSet']
print data

If I run this before visiting the website on a browser I receive this error:

Traceback (most recent call last):
File "stackOverflow.py", line 5, in <module>
response.raise_for_status() # raise exception if invalid response
File "C:\Python27\lib\site-packages\requests\models.py", line 851, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request

But after I visit the url in chrome and then run it again I get the list I was hoping for.

My first thought is that maybe the NBA is blocking some types of requests, could that be it?

Thanks for your help.

Answer 1


Pass a user-agent and it will work :

u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url, headers={"USER-AGENT":u_a})