__author__='yc'
import urllib,urllib2
import ssl
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
# 目的为了通过未认证的12306证书
ssl._create_default_https_context=ssl._create_unverified_context
def login():
#获取12306验证码图片地址的请求
req=urllib2.Request(
'https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.6551712691897946'
)
req.headers=headers
imgCode=urllib2.urlopen(req).read()
with open('code.png','wb')as fn:
fn.write(imgCode)
req = urllib2.Request(
'https://kyfw.12306.cn/passport/captcha/captcha-check'
)
req.headers=headers
html=urllib2.urlopen(req).read()
print html
login()