12306登录爬虫 cookies版本
import requests import re import base64 cookies = None # 进入主页,保留cookies login_url = 'https://kyfw.12306.cn/otn/resources/login.html' headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3664.3 Safari/537.36' } data = { } res = requests.get(login_url,headers = headers) cookies = res.cookies # print(res.cookies) #验证码图片下载 pic_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image64?login_site=E&module=login&rand=sjrand&1547541563360&callback=jQuery191024188047659526446_1547541316425&_=1547541316427' res_pic = requests.get(pic_url,headers = headers,cookies = cookies) cookies = res_pic.cookies pic_data = re.findall(b'image":"(.*?)"',res_pic.content)[0] # print(res_pic.content) # print(pic_data) pic_64d = base64.b64decode(pic_data) # print(pic_64d) with open('img/YanZheng.jpg','wb') as f: f.write(pic_64d) # 验证图片 def turn(): a = input('请输入:') numli = a.split(',') result = [] for i in numli: i = int(i) if i == 1: result.append('42,45') if i == 2: result.append('120,45') if i == 3: result.append('198,45') if i == 4: result.append('262,45') if i == 5: result.append('42,102') if i == 6: result.append('118,102') if i == 7: result.append('177,102') if i == 8: result.append('262,102') result = ','.join(result) return result data2 = { 'callback':'jQuery191046501253654005814_1547541829587', 'answer':turn(), 'rand':'sjrand', 'login_site':'E', '_':'1547541829591' } # print(data2) Yan_url = 'https://kyfw.12306.cn/passport/captcha/captcha-check' res_Yan = requests.get(Yan_url,params = data2,headers = headers,cookies = cookies) print(res_Yan.url) res_Yan.encoding = 'utf-8' print(res_Yan.text)