[Python] 纯文本查看 复制代码
import requests
from PIL import Image
import json
answer = {
"1": "40,40",
"2": "110,40",
"3": "180,40",
"4": "260,40",
"5": "40,120",
"6": "110,120",
"7": "180,120",
"8": "260,120",
}
head = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299",
"Host": "kyfw.12306.cn",
"Referer": "https://kyfw.12306.cn/otn/resources/login.html"
}
def get_img_code():
img_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand'
img = session.get(url=img_url, headers=head).content
with open('code.jpg', 'wb') as f:
f.write(img)
def check_img():
check_url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
img = Image.open('code.jpg').show()
print("+----------+----------+----------+----------+")
print("| 1 | 2 | 3 | 4 |")
print("|----------|----------|----------|----------|")
print("| 5 | 6 | 7 | 8 |")
print("+----------+----------+----------+----------+")
input_code = input("请在1—8中选择输入验证图片编号,以半角','隔开。(例如:1,3,5):")
answer_code = ''
try:
for i in input_code.split(','):
answer_code += ',' + answer[i] if (i is not input_code[0]) else answer[i]
except Exception as e:
print('输入错误请重新输入!')
check_img()
data = {
'answer': answer_code,
'login_site': 'E',
'rand': 'sjrand',
}
check_result = session.post(url=check_url, data=data, headers=head).text
check_result = json.loads(check_result)
print(check_result)
if check_result['result_code'] == '4':
print('*' * 20 + '验证码正确' + '*' * 20)
login(answer_code)
else:
get_img_code()
check_img()
def login(answer_code):
# name = input('账号:')
# pwd = input('密码:')
name = '*********'
pwd = '*********'
login_url = 'https://kyfw.12306.cn/passport/web/login'
formdata = {
'username': name,
'password': pwd,
'appid': 'otn',
'answer': answer_code
}
login_result = session.post(url=login_url, data=formdata, headers=head)
login_result.encoding='utf-8'
print(login_result.text)
with open('3.html','w',encoding='utf-8') as f:
f.write(login_result.text)
# def user_login():
# url='https://kyfw.12306.cn/otn/login/userLogin'
# page=requests.get(url=url).text
# print(page)
# def check_useer():
# url = 'https://kyfw.12306.cn/otn/login/checkUser'
# data = {
# '_json_att': ''
# }
# requests.post(url=url, data=data,headers=head)
def uamtk():
url = 'https://kyfw.12306.cn/passport/web/auth/uamtk'
data = {
'appid': 'otn',
}
uamtk_page = session.post(url=url, data=data, headers=head).text
print(uamtk_page)
if __name__ == '__main__':
session = requests.Session()
get_img_code()
check_img()
uamtk()