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')"))
Changing the User-Agent
to different one I was able to get the 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)
Prints:
<!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>
...
10060
seems to be an error coming from low-latency connections.
From sources, the best fix is to either speed-up your connection by disabling any downloads
you may be having or by adding timeouts
to your connection(s) this can be done in many ways depending on how you're trying to access the remote hosts.
Luckily, the requests library got that ability: Your code should look like this:
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>
Documentation for Error 10060: 10060 Connection Error
Documentation for Timeouts: Quickstart - Requests.Timeouts
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.