准备写一个 四川票务网的 检测票自动买汽车票功能,结果登录不上悲伤,继续研究
# -*- coding: utf-8 -*- #encoding=utf-8 import requests import re,time,random from PIL import Image session = requests.session() def login(id,password,tonken): #post需要的表单数据,类型为字典 xiaoshu='0.' + str(random.randint(1,9)) +str(random.randint(12345678901234567,99999999999999999)) # urlretrieve('https://www.scqckypw.com:443/rCode.jpg?d=0.4634 0046 7176 8108', 'yanzhenma.png') r = requests.get('https://www.scqckypw.com:443/rCode.jpg?d='+ xiaoshu) with open('yanzhenma.png', 'wb') as f: f.write(r.content) f=Image.open('yanzhenma.png') f.show() # yanzhenma=input("验证码") yanzhenma='2feg' # https://www.scqckypw.com:443/rCode.jpg?d=0.8352819311928219 login_data = { 'uname': id, 'passwd': password, 'code': yanzhenma, 'tonken': tonken, } # Response Heades响应头 Response_heades={ 'Connection': 'keep-alive', 'Content-Length': '70', 'Content-Type': 'text/html;charset=UTF-8', 'Date': 'Mon, 22 Apr 2019 07:34:50 GMT', 'Server': 'Tengine', 'Set-Cookie': 'lname=""; Path=/', } #设置头信息Request Heades请求标题 headers_base = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', # 'Content-Length': '70', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': 'UM_distinctid=16a43e85f60161-096088bb31b458-5a40201d-1fa400-16a43e85f6222e; CNZZDATA1254367912=1097983507-1555917477-%7C1555917477; lname=""; JSESSIONID=ABE9100054D0D562DDE01444EB54CE5E', 'Host': 'www.scqckypw.com', 'Origin': 'https://www.scqckypw.com', 'Referer': 'https://www.scqckypw.com/login/index.html', 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest', } #使用seesion登录,这样的好处是可以在接下来的访问中可以保留登录信息 #登录的URL # baseurl += "/login/email" #requests 的session登录,以post方式,参数分别为url、headers、data content = session.post('https://www.scqckypw.com/login/check.json', headers = headers_base, data = login_data) #成功登录后输出为 {"r":0,#"msg": "\u767b\u9646\u6210\u529f"#} print(content.content.decode('utf8')) # time.sleep(13) # user_content = session.post('https://www.scqckypw.com:443/user/index.html', headers = headers_base) # print(user_content.content.decode('utf8')) url = "https://www.scqckypw.com/login/index.html" id='17051329118' password='wodemima' tonken = re.compile('.*?<input type="hidden" id="csrfmiddlewaretoken1" name="csrfmiddlewaretoken" value="(.*?)">.*?') content = session.get(url).text tonken=re.findall(tonken,content)[0] login(id,password,tonken)
现在还停留在非法提交界面,肯能是cooike还没解决好吧,慢慢学习研究,当然大佬指点一二更好谢谢