Tricking the Nasdaq API to talk to your python code in JSON
When people need to download ticker symbols from Nasdaq, many will attempt to access the api.nasdaq.com API endpoint. For instance, making a request to this endpoint provides a comprehensive JSON response containing information about all ticker symbols trading on Nasdaq.
https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=25&offset=0&download=true
However, while you can easily view this JSON response in your browser, attempting the same operation with Python may encounter an issue. The Nasdaq server might become unresponsive, leading to delays and waiting times.
import requests
def tickersNasdaq():
url = "https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=25&offset=0&download=true"
response = requests.get(url)
print("Response:")
print(response.text)
# Example usage
tickersNasdaq()
The above code will result in a unresponsive logic.
Be a human not a machine
The problem is that the API is checking for the user agent, if we prentend to be a human and not code the Nasdaq server will respond without fail. The below code will work without any problem as we ‘fake’ that we are a human user by defining a user_agent.
import requests
def…