Created
August 9, 2013 03:44
-
-
Save YoukouTenhouin/6191023 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getToken(session): | |
session.get('http://www.baidu.com') | |
token = session.get('https://passport.baidu.com/v2/api/?getapi&class=login&tpl=pp&tangram=false').text | |
token_index = token.index('login_token')+len('login_token')+2 | |
token = token[token_index:-1] | |
token = token[0:token.index("'")] | |
return token | |
def getvcode(codestring): | |
url = 'https://passport.baidu.com/cgi-bin/genimage?'+codestring | |
r = requests.get(url).content | |
with open('vcode.gif','wb') as f: | |
f.write(r) | |
p = Popen(['feh','vcode.gif']) | |
ret = input('vcode?') | |
p.terminate() | |
return ret | |
def login(username,password): | |
session = requests.session() | |
# Check if we have to deal with verify codes | |
get_params = { | |
'tpl':'pp', | |
'callback':'bdPass.api.login._needCodestringCheckCallback', | |
'index':0, | |
'logincheck':'', | |
'time':0, | |
'username':username | |
} | |
# Ask server | |
r = session.get('https://passport.baidu.com/v2/api/?logincheck',params=get_params) | |
# Callback for verify code if we need | |
codestring = r.text[r.text.index('(')+1:r.text.index(')')] | |
codestring = json.loads(codestring)['codestring'] | |
codestring = codestring if codestring != "null" else None | |
verifycode = (getvcode(codestring)) if codestring != None else "" | |
# Now we'll do login | |
# Get token | |
session.get('http://www.baidu.com') | |
token = session.get('https://passport.baidu.com/v2/api/?getapi&class=login&tpl=pp&tangram=false').text | |
token_index = token.index('login_token')+len('login_token')+2 | |
token = token[token_index:-1] | |
token = token[0:token.index("'")] | |
# Construct post body | |
post_data = { | |
'token':token, | |
'ppui_logintime':'1600000', | |
'charset':'utf-8', | |
'codestring':codestring, | |
'isPhone':'false', | |
'index':0, | |
'u':'', | |
'safeflg':0, | |
'staticpage':'http://www.baidu.com/cache/user/html/jump.html', | |
'loginType':1, | |
'tpl':'pp', | |
'callback':'parent.bd__pcbs__qvljue', | |
'username':username, | |
'password':password, | |
'verifycode':verifycode, | |
'mem_pass':'on', | |
'apiver':'v3' | |
} | |
# Post! | |
# XXX : do not handle errors | |
r = session.post('https://passport.baidu.com/v2/api/?login',data=post_data) | |
return session |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment