马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 幽梦三影 于 2018-7-11 18:35 编辑
12306的验证码靠的是捕捉鼠标坐标来验证的
可通过qq截图从第一个点移动到第二个点查看坐标
from http.cookiejar import CookieJar
import urllib.request as u
import urllib.parse
import json
import easygui as e
url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
cookie = CookieJar()
opener = u.build_opener(u.HTTPCookieProcessor)
img_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.2303874087852643'
headers = {'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"}
rep = u.Request(img_url,headers=headers)
html = opener.open(rep).read()
with open('验证码.png','wb') as f:
f.write(html)
num = e.enterbox(msg='请输入验证码坐标',image='验证码.png')
data = {
'answer':num,
'login_site':'E',
'rand':'sjrand'
}
data = urllib.parse.urlencode(data).encode('utf-8')
rep = u.Request(url,headers=headers)
html = opener.open(rep,data=data).read().decode('utf-8')
if json.loads(html)['result_code'] == '4':
print('验证码校验成功')
user = []
title = '登录'
msg = '请输入用户名和密码'
fields = ['用户名','密码']
user = e.multpasswordbox(msg,title,fields=fields)
form = {
'appid':'otn',
'password':user[1],
'username':user[0]
}
log_url = 'https://kyfw.12306.cn/passport/web/login'
form = urllib.parse.urlencode(form).encode('utf-8')
rep = u.Request(log_url,headers=headers)
html = opener.open(rep,data=form).read().decode('utf-8')
if json.loads(html)['result_code'] == 0:
print('登录成功')
else:
print('登录失败')
else:
print('验证码校验失败')
|