Accessing NASDAQ Historical Data with Python Requests Results in Connection Timeout

Viewed 1272

I tried to run this snippet of Python code (with the requests library) to retrieve a year's worth of Tesla's historical market data from NASDAQ.com

dataURL = https://www.nasdaq.com/api/v1/historical/TSLA/stocks/2019-05-22/2020-05-21
quotesReq = requests.get(dataURL, allow_redirects = True)

Despite being able to access the URL through a web browser and downloading the intended ".csv" file, my Python code produces a timeout error.

requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None))

Is this because NASDAQ.com has anti-scraping measures in place, or am I missing some information in my request? Any help fixing the issue would be appreciated.

2 Answers

Try by adding these three headers:

"Accept-Language":"en-US,en;q=0.9"
"Accept-Encoding":"gzip, deflate, br"
"User-Agent":"Java-http-client/"

Here is a simple solution with using Selenium chromedriver.

import time
from selenium import webdriver

dataURL = 'https://www.nasdaq.com/api/v1/historical/TSLA/stocks/2019-05-22/2020-05-21'
driver = webdriver.Chrome('C:/chromedriver.exe')  
driver.get(dataURL)
time.sleep(5)
driver.quit()

Again, check with the provider for terms on proper scraping. You can look into more of selenium chromewebdriver, https://chromedriver.chromium.org/getting-started

Related