一.问题描述
vscode利用requests库请求CSDNget类型接口报错ValueError: check_hostname requires server_hostname
代码如下:
'''
requests的基础使用-post请求
'''
import requests
# print('hello requests')
'''
requests库发送get参数---fiddler抓包,CSDN查看我的粉丝
'''
get_url1 = 'https://blog.csdn.net/community/home-api/v1/get-fans-list'
data_get = {
'page' : 1,
'size' : 20,
'noMore' : False,
'blogUsername' : 'xun_zhao_t521'
}
header = {
'Host': 'blog.csdn.net',
'Connection': 'keep-alive',
'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
'Accept': 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
'X-Tingyun-Id': 'C0UKgY0m8NI;r=713972543',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://blog.csdn.net/xun_zhao_t521?type=sub&subType=fans&spm=1008.2221.3001.5347',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': ssxmod_itna2=eq0xgDuD07GQYAIK0dD=wgD7TLfTKaaDy0QOQDxnI3R5Dse+DLnQ4=NQQt8DZ+qrUrXnWdvIHqfmL2ipqIOCydqu7b1nam2IEBEuAkzEZda2geZRCtPXDn4tuesyYSkhW2M1KeyXwRjtGd0OQDthRdEyQ3NSISEyj8tBYGX4Yldu/x0M0+Uxe+rUSCIB+nEXjwTh/+u4qPtwAlW8pwnFp+Ukcq5DoZUAKxx4Xj4BCSy7LuW2YSLskBXWp8r7sZRPu1QDTQR6xpXrKA7a5hGRuxMXf7SdqvF7rVHutIgshGlsatoAqkEKjIIZeVr0xtI4+Ee/we+UG2pmPIv10mDd+i6; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22xun_zhao_t521%22%2C%22scope%22%3A1%7D%7D; dc_session_id=10_1624711700473.526426; c_first_page=https%3A//blog.csdn.net/qq_2300688967/article/details/79028304; announcement-c_ref=https%3A//mall.csdn.net/myorder%3Fspm%3D1011.2124.3001.5137; c_page_id=default; dc_tos=qvb9bo; log_Id_pv=417'
}
res2 = requests.get(get_url1,data_get,headers=header,verify=False).text
print(res2)
具体报错信息如下:
Traceback (most recent call last):
File "one_day.py", line 50, in <module>
res2 = requests.get(get_url1,data_get,headers=header,verify=False).text
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
self._prepare_proxy(conn)
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
conn.connect()
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\urllib3\connection.py", line 359, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\urllib3\connection.py", line 500, in _connect_tls_proxy
return ssl_wrap_socket(
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "D:\D1\IDEs\Python3.8.5\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "D:\D1\IDEs\Python3.8.5\lib\ssl.py", line 997, in _create
raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname
显示错误信息对应代码
二.问题解决
1.在网上查询知在下载requests插件,会自动的下载安装urllib3这个插件,且对应版本太高,就导致了这个报错。
可以查看下版本:
(这里我是修改之后的,所以展示的是低版本)
于是尝试将urllib3版本降低
pip install urllib3==1.25.11
于是用管理员身份打开cmd
输入命令,结果还是报错:
2.尝试为插件下载指定源,仍然报错
C:\WINDOWS\system32>pip install urllib3==1.25.8 -i http://mirrors.aliyun.com/pypi/simple
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.
ERROR: Could not find a version that satisfies the requirement urllib3==1.25.8 (from versions: none)
ERROR: No matching distribution found for urllib3==1.25.8
3.又尝试关闭我的代理
4.重新打开命令窗口,重新执行命令
C:\WINDOWS\system32>pip install urllib3==1.25.8
Collecting urllib3==1.25.8
Downloading urllib3-1.25.8-py2.py3-none-any.whl (125 kB)
|████████████████████████████████| 125 kB 656 kB/s
Installing collected packages: urllib3
Attempting uninstall: urllib3
Found existing installation: urllib3 1.26.6
Uninstalling urllib3-1.26.6:
Successfully uninstalled urllib3-1.26.6
Successfully installed urllib3-1.25.8
发现成功安装
5.重新vscode运行代码,成功输出接口返回信息
版权声明:本文为xun_zhao_t521原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。