|
import requests as rs
import time
import re
import datetime
import random
import urllib.parse as p
class login:
def __init__(self,username,password):
self.username=username
self.password=password
self.se=rs.session()
self.data={}
self.data['ppui_logintime']=random.randint(10000,9999999)
self.data['charset']='utf-8'
self.data['codestring']=self.get_codestring()
self.data['token']=self.get_token()
self.data['isPhone']='false'
self.data['index']='0'
self.data['u']=''
self.data['safeflg']='0'
self.data['staticpage']='http%3A%2F%2Fwww.baidu.com%2Fcache%2Fuser%2Fhtml%2Fjump.html'
self.data['loginType']='1'
self.data['tpl']='mn'
self.data['callback']='parent.bdPass.api.login._postCallback'
self.data['username']=self.username
self.data['password']=self.password
self.data['verifycode']=self.get_verify()
self.data['mem_pass']='on'
self.head = {}
self.head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko' \
') Chrome/55.0.2883.87 Mobile Safari/537.36'
print(self.data)
def get_codestring(self):
url='https://passport.baidu.com/v2/api/?logincheck&callback=bdPass.api.login._needCodestringCheckCallback&tpl=' \
'mn&charset=UTF-8&index=0&username='+self.username+'&isphone=false&time='+self.get_posttime()
html = self.se.get(url).text
match = re.compile("codestring\":\"(.*?)\"")
codestring = match.findall(html)[0]
return codestring
def get_token(self):
rs.get('http://news.baidu.com/')
url='https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true'
html=self.se.get(url).text
match = re.compile("token='(.*?)'")
token = match.findall(html)[0]
return token
def get_verify(self):
url='https://passport.baidu.com/cgi-bin/genimage?'+self.data['codestring']+'&v='+self.get_posttime()
html=self.se.get(url).content
with open(r'C:\Users\Mr-zhang\Desktop\pic.png','wb') as f:
f.write(html)
verify=input('请输入验证码')
verify={'verify':verify}
verify=p.urlencode(verify)[7:]
return verify
def get_posttime(self):
now_time = str(int(time.mktime(datetime.datetime.now().timetuple())))
return now_time
def main(self):
post_url='https://passport.baidu.com/v2/api/?login'
response=self.se.post(post_url,data=self.data,headers=self.head).text
print(response)
if __name__ == '__main__':
s=login('******','******')
s.main()上面是python代码,提示 https:\/\/passport.baidu.com\/v2Jump.html?callback=parent.bdPass.api.login._postCallback&index=0&codestring=tcG1107e245a01dc1ba02e914ce9801ac7b34a444061f043196&username=+‘用户名’+&phonenumber=&mail=&tpl=mn&u=https%3A%2F%2Fpassport.baidu.com%2F&needToModifyPassword=&gotourl=&auth=&error=6');
|
|