通过chrome可以得到以下信息:
1.验证码校验地址 https://kyfw.12306.cn/passport/captcha/captcha-check
2.参数 answer:252,120
基础知识
req = urllib.request.Request('https://www.qiushibaike.com/') #实例一个请求对象
res = urllib.request.urlopen(req) #请求url
res.geturl() #获取最终的url,用于判别url是否跳转
urllib.parse.urlencode() #查询字符串生成 参数生成
遇到的问题
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
解决方案
-
使用ssl创建未经验证的上下文,在urlopen中传入上下文参数
context = ssl._create_unverified_context()
codeimg = urllib.request.urlopen(req,context=context).read()
-
全局取消证书验证
ssl._create_default_https_context = ssl._create_unverified_context
or
_create_unverified_https_context = ssl._create_unverified_context
最后urllib访问
codeimg = urllib.request.urlopen(req).read()
- 使用requests库可以不设定上述的全局变量
codeimg = requests.get(req, verify=False).content
import urllib.parse
import ssl
import http.cookiejar
c = http.cookiejar.LWPCookieJar() # 生成一个储存cookies的对象
cookie = urllib.request.HTTPCookieProcessor(c)
opener = urllib.request.build_opener(cookie)
urllib.request.install_opener(opener) #可加可不加
#context = ssl._create_unverified_context() #这个方法在opener.open()中失效
ssl._create_default_https_context = ssl._create_unverified_context #全局生效
req = urllib.request.Request('https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.07182472007725394')
#codeimg = urllib.request.urlopen(req,context=context).read()
codeimg = opener.open(req).read()
print(codeimg)
fn = open('code.png','wb')
fn.write(codeimg)
fn.close()
req = urllib.request.Request('https://kyfw.12306.cn/passport/captcha/captcha-check')
code = input('>>')
data = {
'answer':code,
'login_site':'E',
'rand':'sjrand',
}
data = urllib.parse.urlencode(data).encode('utf-8') #字典格式转换成查询字符串
# data = 'answer=252%2C120&login_site=E&rand=sjrand'
#html = urllib.request.urlopen(req,data=data,context=context).read().decode('utf-8')
html =opener.open(req,data=data).read().decode('utf-8')
print(html)
image.png
网友评论