- A+
所属分类:Python
首先描述详细的错误, 贴段代码:
import requests# import warnings# warnings.filterwarnings("ignore")headers = { 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Host': "www.cqepc.cn", 'DNT': "1", 'Pragma': "no-cache", 'Upgrade-Insecure-Requests': "1", 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}url = "https://www.cqepc.cn/aspcms/newslist/list-255-1.html"proxies = Noneres = requests.get("https://www.cqepc.cn/aspcms/newslist/list-255-1.html", headers=headers, verify=False, proxies=proxies)print(res.status_code)print(res.headers)print(res.content.decode("gb2312"))
运行报错结果报错如下:
Traceback (most recent call last): File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 456, in wrap_socket cnx.do_handshake() File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\SSL.py", line 1915, in do_handshake self._raise_ssl_error(self._ssl, result) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\SSL.py", line 1640, in _raise_ssl_error raise SysCallError(-1, "Unexpected EOF")OpenSSL.SSL.SysCallError: (-1, 'Unexpected EOF')During handling of the above exception, another exception occurred:Traceback (most recent call last): File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen chunked=chunked) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 343, in _make_request self._validate_conn(conn) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 839, in _validate_conn conn.connect() File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py", line 344, in connect ssl_context=context) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\ssl_.py", line 347, in ssl_wrap_socket return context.wrap_socket(sock, server_hostname=server_hostname) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 462, in wrap_socket raise ssl.SSLError('bad handshake: %r' % e)ssl.SSLError: ("bad handshake: SysCallError(-1, 'Unexpected EOF')",)During handling of the above exception, another exception occurred:Traceback (most recent call last): File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 449, in send timeout=timeout File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\retry.py", line 399, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause))urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.cqepc.cn', port=443): Max retries exceeded with url: /aspcms/newslist/list-255-1.html (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))During handling of the above exception, another exception occurred:Traceback (most recent call last): File "C:/Users/shuquanqiang/Documents/WeChat Files/li8615625/FileStorage/File/2019-08/chongqinghangtian(1).py", line 32, in <module> res = requests.get("https://www.cqepc.cn/aspcms/newslist/list-255-1.html", headers=headers, verify=False, proxies=proxies) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\api.py", line 75, in get return request('get', url, params=params, **kwargs) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "C:\Users\shuquanqiang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 514, in send raise SSLError(e, request=request)requests.exceptions.SSLError: HTTPSConnectionPool(host='www.cqepc.cn', port=443): Max retries exceeded with url: /aspcms/newslist/list-255-1.html (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))Process finished with exit code 1
然后进行了好多尝试,发现 当打开代理charles进行抓包时,代码竟然可以请求通,但是总不能一直开着代理啊,继续查找,尝试了网上好多代码,最终通了,代码如下:
#!/usr/bin/env python# -*- coding:utf-8 -*-import sslimport requestsfrom requests.adapters import HTTPAdapterfrom requests.packages.urllib3.poolmanager import PoolManager#from urllib3.poolmanager import PoolManagerfrom requests.packages.urllib3.util.ssl_ import create_urllib3_context headers = { 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Host': "www.cqepc.cn", 'DNT': "1", 'Pragma': "no-cache", 'Upgrade-Insecure-Requests': "1", 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}url = "https://www.cqepc.cn/aspcms/newslist/list-255-1.html"# py3.6CIPHERS = ( 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:' 'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:' '!eNULL:!MD5')class DESAdapter(HTTPAdapter): """ A TransportAdapter that re-enables 3DES support in Requests. """ def create_ssl_context(self): #ctx = create_urllib3_context(ciphers=FORCED_CIPHERS) ctx = ssl.create_default_context() # allow TLS 1.0 and TLS 1.2 and later (disable SSLv3 and SSLv2) #ctx.options |= ssl.OP_NO_SSLv2 #ctx.options |= ssl.OP_NO_SSLv3 #ctx.options |= ssl.OP_NO_TLSv1 ctx.options |= ssl.OP_NO_TLSv1_2 ctx.options |= ssl.OP_NO_TLSv1_1 #ctx.options |= ssl.OP_NO_TLSv1_3 ctx.set_ciphers( CIPHERS ) #ctx.set_alpn_protocols(['http/1.1', 'spdy/2']) return ctx def init_poolmanager(self, *args, **kwargs): context = create_urllib3_context(ciphers=CIPHERS) kwargs['ssl_context'] = self.create_ssl_context() return super(DESAdapter, self).init_poolmanager(*args, **kwargs) def proxy_manager_for(self, *args, **kwargs): context = create_urllib3_context(ciphers=CIPHERS) kwargs['ssl_context'] = self.create_ssl_context() return super(DESAdapter, self).proxy_manager_for(*args, **kwargs)proxies=Noneses = requests.session()ses.mount('https://', DESAdapter())response = ses.get(url, headers=headers, proxies=proxies, verify=True)print(response.status_code)print(response.content.decode("gb2312"))
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫