您好,欢迎访问一九零五行业门户网

使用python模拟登陆百度

使用python模拟登陆百度
#!/usr/bin/python # -*- coding: utf-8 -*- import re; import cookielib; import urllib; import urllib2; import optparse; #------------------------------------------------------------------------------ # check all cookies in cookiesdict is exist in cookiejar or not def checkallcookiesexist(cookienamelist, cookiejar) : cookiesdict = {}; for eachcookiename in cookienamelist : cookiesdict[eachcookiename] = false; allcookiefound = true; for cookie in cookiejar : if(cookie.name in cookiesdict) : cookiesdict[cookie.name] = true; for eachcookie in cookiesdict.keys() : if(not cookiesdict[eachcookie]) : allcookiefound = false; break; return allcookiefound; #------------------------------------------------------------------------------ # just for print delimiter def printdelimiter(): print '-'*80; #------------------------------------------------------------------------------ # main function to emulate login baidu def emulateloginbaidu(): print function: used to demostrate how to use python code to emulate login baidu main page: http://www.baidu.com/; print usage: emulate_login_baidu_python.py -u yourbaiduusername -p yourbaidupassword; printdelimiter(); # parse input parameters parser = optparse.optionparser(); parser.add_option(-u,--username,action=store,type=string,default='',dest=username,help=your baidu username); parser.add_option(-p,--password,action=store,type=string,default='',dest=password,help=your baidu password); (options, args) = parser.parse_args(); # export all options variables, then later variables can be used for i in dir(options): exec(i + = options. + i); printdelimiter(); print [preparation] using cookiejar & httpcookieprocessor to automatically handle cookies; cj = cookielib.cookiejar(); opener = urllib2.build_opener(urllib2.httpcookieprocessor(cj)); urllib2.install_opener(opener); printdelimiter(); print [step1] to get cookie baiduid; baidumainurl = http://www.baidu.com/; resp = urllib2.urlopen(baidumainurl); #respinfo = resp.info(); #print respinfo=,respinfo; for index, cookie in enumerate(cj): print '[',index, ']',cookie; printdelimiter(); print [step2] to get token value; getapiurl = https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true; getapiresp = urllib2.urlopen(getapiurl); #print getapiresp=,getapiresp; getapiresphtml = getapiresp.read(); #print getapiresphtml=,getapiresphtml; #bdpass.api.params.login_token='5ab690978812b0e7fbbe1bfc267b90b3'; foundtokenval = re.search(bdpass\.api\.params\.login_token='(?p\w+)';, getapiresphtml); if(foundtokenval): tokenval = foundtokenval.group(tokenval); print tokenval=,tokenval; printdelimiter(); print [step3] emulate login baidu; staticpage = http://www.baidu.com/cache/user/html/jump.html; baidumainloginurl = https://passport.baidu.com/v2/api/?login; postdict = { #'ppui_logintime': , 'charset' : utf-8, #'codestring' : , 'token' : tokenval, #de3dbf1e8596642fa2ddf2921cd6257f 'isphone' : false, 'index' : 0, #'u' : , #'safeflg' : 0, 'staticpage' : staticpage, #http%3a%2f%2fwww.baidu.com%2fcache%2fuser%2fhtml%2fjump.html 'logintype' : 1, 'tpl' : mn, 'callback' : parent.bdpass.api.login._postcallback, 'username' : username, 'password' : password, #'verifycode' : , 'mem_pass' : on, }; postdata = urllib.urlencode(postdict); # here will automatically encode values of parameters # such as: # encode http://www.baidu.com/cache/user/html/jump.html into http%3a%2f%2fwww.baidu.com%2fcache%2fuser%2fhtml%2fjump.html #print postdata=,postdata; req = urllib2.request(baidumainloginurl, postdata); # in most case, for do post request, the content-type, is application/x-www-form-urlencoded req.add_header('content-type', application/x-www-form-urlencoded); resp = urllib2.urlopen(req); #for index, cookie in enumerate(cj): # print '[',index, ']',cookie; cookiestocheck = ['bduss', 'ptoken', 'stoken', 'saveuserid']; loginbaiduok = checkallcookiesexist(cookiestocheck, cj); if(loginbaiduok): print +++ emulate login baidu is ok, ^_^; else: print --- failed to emulate login baidu ! else: print fail to extract token value from html=,getapiresphtml; if __name__==__main__: emulateloginbaidu();
其它类似信息

推荐信息