python3爬ssl的方法

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib.request
import chardet
import ssl

# 2. 表示忽略未经核实的SSL证书认证

context = ssl._create_unverified_context()

# 3. 在urlopen()方法里 指明添加 context 参数
page=urllib.request.urlopen('http://www.baidu.com',context=context)
htmlCode=page.read()
#print(chardet.detect(htmlCode)) #查看编码方式
data=htmlCode.decode('utf-8')
#print(data) #打印网页源代码
pageFile=open('pageCode.txt','wb')#以写的方式打开pageCode.txt
pageFile.write(htmlCode)#写入
pageFile.close()#开了记得关
原文地址:https://www.cnblogs.com/alone2015/p/12427018.html