python3处理HTTPS请求 SSL证书验证12306网站
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())
简洁快速方法:
from urllib.request import urlopen
import ssl
url = "https://www.12306.cn/mormhweb/"
ssl._create_default_context = ssl._create_unverified_context
resp = urlopen(url)
print(resp.read().decode('utf-8')