Johan Hayoun
Johan Hayoun

Reputation: 3

Can't open an URL in Python without opened it in browser first

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

Upvotes: 0

Views: 1035

Answers (1)

alexisdevarennes
alexisdevarennes

Reputation: 5642

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

Upvotes: 1

Related Questions