使用Python请求访问NASDAQ历史数据会导致连接超时

使用Python请求访问NASDAQ历史数据会导致连接超时,python,python-requests,finance,Python,Python Requests,Finance,我试图运行这段Python代码(带有请求库),从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) 尽管能够通过web浏览器访问URL并下载预期的“.csv”文件,但我的Python代码仍会产生超时错误 reque

我试图运行这段Python代码(带有请求库),从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)
尽管能够通过web浏览器访问URL并下载预期的“.csv”文件,但我的Python代码仍会产生超时错误

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))

这是因为NASDAQ.com有反刮削措施,还是我的请求中遗漏了一些信息?如果您能帮助解决此问题,我们将不胜感激。

这是一个使用硒铬驱动的简单解决方案

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()

同样,请与供应商联系,了解有关正确刮削的条款。您可以查看selenium chromewebdriver的更多信息,请尝试添加以下三个标题:

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

可能是因为网站有防刮措施。不管怎么说,抓取一个网站被视为一种失礼行为,即使它们没有为您提供获取所需数据的API。请尝试将您的请求头的用户代理设置为某种浏览器,看看它是否有什么不同。@Makoto我重新整理了我的其余代码,现在得到了“ConnectionResetError:[WinError 10054]现有连接被远程主机强制关闭”,这似乎是超时错误的首要原因。你对此有什么建议吗?@noobius不幸的是,它与以下标题没有区别:headers={“用户代理”:“Mozilla/5.0(Windows;U;Windows NT 5.1;en-US;rv:1.9.2.8)Gecko/20100722 Firefox/3.6.8 GTB7.1(.NET CLR 3.5.30729)”,“Referer”:“}你研究过Selenium webdriver吗?使用类似selenium chrome的驱动程序可能会起作用。那是我下一步要去的地方。但是,请咨询提供商,了解他们的刮削条款。