Import Urllib2url = "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) Response = Urllib2.urlopen (request) print response.read ()
Operation Result:
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>
Therefore, if you encounter such a site in the future, we need to handle the SSL certificate separately, let the program ignore the SSL certificate validation error, can be accessed normally.
Import Urllibimport urllib2# 1. Import Python SSL processing module import ssl# 2. Indicates ignoring unverified SSL certificate authentication 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. Specify the Add context parameter in the Urlopen () method response = Urllib2.urlopen (Request, context = context) Print Response.read ()
HTTPS Request SSL Certificate validation