背景
近日在写Python爬虫解析网页,进行文章信息的获取、文件下载等
问题
突然遇到问题
urllib.error.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:777)>
解决方法
得挂载http时指定一个adapter,让sslVersion为TLSV1。新增代码如下
from requests.adapters import HTTPAdapter
from urllib3.poolmanager import PoolManager
import ssl
class MyAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize,
block=block,
ssl_version=ssl.PROTOCOL_TLSv1)
if __name__ == '__main__':
s = requests.Session()
s.mount('https://', MyAdapter())
.. # 老逻辑
结语
参考文献
python3 使用urllib报错urlopen error EOF occurred in violation of protocol (_ssl.c:841)