python3处理HTTPS请求 SSL证书验证12306网站

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z564359805/article/details/84535600
import urllib.request
# 1. 导入Python SSL处理模块
import ssl

urllib2 = urllib.request
# 2. 表示忽略未经核实的SSL证书认证
context = ssl._create_unverified_context()
url = "https://www.12306.cn/mormhweb/"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
request = urllib2.Request(url, headers = headers)
# 3. 在urlopen()方法里 指明添加 context 参数
response = urllib2.urlopen(request, context = context)
print(response.read().decode())

猜你喜欢

转载自blog.csdn.net/z564359805/article/details/84535600
今日推荐