首页 > 解决方案 > TimeoutError: [WinError 10060] 连接尝试失败

问题描述

TimeoutError: [WinError 10060] 连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应

我做了我能想到的一切来解决这个问题,但也许我没有完全理解这个错误。请帮帮我。

这是代码和链接。

import urllib.request
import requests

link = "https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=242&symbol=RELIANCE&symbol=RELIANCE&instrument=OPTSTK&date=-segmentLink=17&segmentLink=17"


user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
request = urllib.request.Request(link,headers={'User-Agent': user_agent})
response = urllib.request.urlopen(request)
html = response.read()
print(html)

标签: pythonpython-3.xurlpython-requestsurllib

解决方案


改用selenium

from selenium import webdriver
import os

browser = webdriver.Chrome(executable_path=os.path.abspath(os.getcwd()) + "/chromedriver")
link = "https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=242&symbol=RELIANCE&symbol=RELIANCE&instrument=OPTSTK&date=-&segmentLink=17&segmentLink=17"
browser.get(link)
context = browser.page_source

推荐阅读