0

I want to make a bot that tweet NBA Scores every day. So I need to get the NBA Scores from the stats.nba website every day. The problem is if I don't click on the JSON link and access it with my browser before trying to open it in my code it doesn't work. There is a new link every day for the matchs of the night.

Does anyone know how to solve that ? Thank you

3
  • 1
    Could you provide the python code you used to access the API? Commented Nov 28, 2019 at 12:51
  • @JanisJansen even a simple f = requests.get(url) print(f.text) doesn't work if I don't open the url first in a browser. Commented Nov 28, 2019 at 13:36
  • To me that sounds a lot like an API/Authorization or CORS issue. You could double-check for the API usage requirements if any form of authorization is required. Commented Nov 28, 2019 at 13:46

1 Answer 1

1

It would be interesting to see your code and figure out why it needs to be opened in the browser first, but if that really is the case:

Just open it with webbrowser first:

import webbrowser
webbrowser.open_new_tab(url)
# rest of your logic below.

This will open the url in your systems default browser.

You could also check if you're missing some flags such as allowing for redirects or if you need an user-agent (so it looks like you're visiting from a browser)

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = requests.get(url, headers=headers, allow_redirects = True)
response.raise_for_status()
content = response.text
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it does work with that (if I wait long enough for the browser to open the page) but I would like a solution that does not open a new tab in a browser, if I plan to host my python code in a server I don't think that would work. even a simple f = requests.get(url) print(f.text) doesn't work if I don't open the url first in a browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.