|
20鱼币
登录12306,一直验证码错误
12306改版之后获取验证码图片的URL后面几段参数有一个一直增加,剩下几个找不到规律啊
有没有大神告知一下我这代码应该怎么改才能登录上 或者告知一下有没有什么错误的地方,代码如下:
import requests
# 获取验证码图片
def get_captcha():
session = requests.Session()
captcha_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image64?login_site=E&module=login&rand=sjrand&1544615751202&callback=jQuery1910743787401637102_1544615373331&_=1544615373338'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36'}
resp = session.get(captcha_url,headers = headers).text
resp = eval(resp[44:-2])
image = str(resp['image'])
img = base64.b64decode(image)
f = open('image.jpg','wb')
f.write(img)
f.close
# 验证图片
def check_captcha():
answer_img = []
answer = ''
site = {'1':'30,45',
'2':'110,43',
'3':'185,47',
'4':'250,48',
'5':'35,120',
"6":'100,117',
'7':'190,121',
'8':'246,122'}
choice = input('请输入验证图片序号,(例如“15”或“347”等):')
choice = list(choice)
for each in choice:
answer_img.append(site[each])
for each in answer_img:
answer += str(each) + ', '
answer = answer[:-1]
return str(answer)
# 登录12306
def login():
login_url = 'https://kyfw.12306.cn/passport/web/login'
username = input('请输入你的用户名:')
password = input('请输入你的密码:')
get_captcha()
session = requests.Session()
data = {'username':username,
'password':password,
'appid':'otn',
'answer':check_captcha()}
headers = {'Host':'kyfw.12306.cn',
'Origin':'https://kyfw.12306.cn',
'Referer':'https://kyfw.12306.cn/otn/resources/login.html',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36'}
resp = session.post(login_url,headers=headers,data=data)
print(resp.text)
login() |
|