Member-only story
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 tickersNasdaq():
url = "https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=25&offset=0&download=true"
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15"
headers = {"User-Agent": user_agent}
response = requests.get(url, headers=headers)
print("Response:")
print(response.text)
# Example usage
tickersNasdaq()
As long as the Nasdaq server thinks we are a human interacting with them via a webbrowser it is more than happy to respond.
About the author(s)
Johan Louwers is currently Chief Enterprise Architect within Oracle as well as the lead architect for NATO and a number of militaries. Johan has a strong and long background in the field of Enterprise Architecture and complex system engineering. Having worked with enterprises in a diverse set of industries as (enterprise) architect, CTO and technical and strategic business advisor Johan brings both deep technical knowledge to the table as well as strong business oriented expertise. In addition to this Johan is a tech addict who tends to enjoy supporting open source initiatives and actively coding as a hobby. Views expressed in this post are personnel and do not necessarily reflect the views of my current employer.