在 python 中使用响应时,我无法从网站获得任何响应。我收到超时错误 10060

i am not able to get any response from the website while using response in python. I am gettting a time out error 10060

import requests
import pandas


def url(index, st_symbol, exp_date):
 
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"}
    page = requests.get('https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date=20AUG2020', headers = headers)

error = ConnectionError: ('Connection aborted.', OSError("(10060, 'WSAETIMEDOUT')"))

网站 - https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date=20AUG2020

10060 似乎是来自 low-latency connections. 的错误 从消息来源来看,最好的解决方法是通过禁用任何 downloads 您可能拥有的 speed-up 您的连接或者通过将 timeouts 添加到您的连接,这可以通过多种方式完成,具体取决于您尝试访问远程主机的方式。

幸运的是,requests 库有这个能力! 您的代码应如下所示:

import requests
import pandas

def url(index, st_symbol, exp_date):
 
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"}
    page = requests.get('https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date=20AUG2020', timeout=0.001, headers=headers) # Timeout can be any value, format: <second>.<milisecond>

错误 10060 的文档:10060 Connection Error

超时文档:Quickstart - Requests.Timeouts

User-Agent 更改为不同的 HTML:

import requests

headers = {
    "User-Agent":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0",
}

page = requests.get('https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date=20AUG2020', headers=headers)
print(page.text)

打印:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

...