E:\Anaconda3\lib\site-packages\urllib3\util\connection.py in create_connection(address, timeout, source_address, socket_options)
95 if err is not None:
---> 96 raise err
97
E:\Anaconda3\lib\site-packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
573 if new_retry.is_exhausted():
--> 574 raise MaxRetryError(_pool, url, error or ResponseError(cause))
575
MaxRetryError: HTTPConnectionPool(host='vip.stock.finance.sina.com.cn', port=80): Max retries exceeded with url: /quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=+1&num=40&sort=symbol&asc=1&node=chgn_730454&symbol=&_s_r_a=+init (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x0000000004E7BF70>, 'Connection to vip.stock.finance.sina.com.cn timed out. (connect timeout=3)'))
During handling of the above exception, another exception occurred:
ConnectTimeout Traceback (most recent call last)
<ipython-input-60-7da0076a46de> in <module>
85 # sw2_url='http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=40&sort=symbol&asc=1&node=sw2_730200&symbol=&_s_r_a=init'
86
---> 87 gg_json=requests.get(url=url,params=params,headers=headers,timeout=(3,5)) #取JSON数据,类型为list
88 gg_data=gg_json.json() #转换成字典
89 for dict in gg_data: #遍历字典取代码和名称
E:\Anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
59 # cases, and look like a memory leak in others.
60 with sessions.Session() as session:
---> 61 return session.request(method=method, url=url, **kwargs)
62
63
E:\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
653
654 # Send the request
--> 655 r = adapter.send(request, **kwargs)
656
657 # Total elapsed time of the request (approximately)
E:\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
502 # TODO: Remove this in 3.0.0: see #2811
503 if not isinstance(e.reason, NewConnectionError):
--> 504 raise ConnectTimeout(e, request=request)
505
506 if isinstance(e.reason, ResponseError):
ConnectTimeout: HTTPConnectionPool(host='vip.stock.finance.sina.com.cn', port=80): Max retries exceeded with url: /quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=+1&num=40&sort=symbol&asc=1&node=chgn_730454&symbol=&_s_r_a=+init (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x0000000004E7BF70>, 'Connection to vip.stock.finance.sina.com.cn timed out. (connect timeout=3)'))
def get_user_agent_pc():
user_agent_pc = [
# 谷歌
'Mozilla/5.0.html (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.html.2171.71 Safari/537.36',
'Mozilla/5.0.html (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.html.1271.64 Safari/537.11',
'Mozilla/5.0.html (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.html.648.133 Safari/534.16',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',
]
return random.choice(user_agent_pc)
hy_id="chgn_730454"
# 各行业成分股页面请求参数
params={
"node": {hy_id},
}
gg_json=requests.get(url=url,params=params,headers=headers,timeout=(3,5))
gg_data=gg_json.json()
for dict in gg_data:
print(dict['name'])
gg_json.close()