python3.3教程之模拟百度登陆代码分享

代码如下:

#-*-coding:utf-8-*-”’created on 2014年1月10日

@author: hhdys”’import urllib.request,http.cookiejar,reclass baidu: def login(self): cj = http.cookiejar.cookiejar() opener = urllib.request.build_opener(urllib.request.httpcookieprocessor(cj)) opener.addheaders = [(‘user-agent’, ‘mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, like gecko) chrome/31.0.1650.63 safari/537.36’)] resp=opener.open(‘http://weigou.baidu.com/’) for c in cj: print(c.name,”====”,c.value) getapiurl = “https://passport.baidu.com/v2/api/?getapi& resp2=opener.open(getapiurl) getapiresphtml = resp2.read().decode(“utf-8”) foundtokenval = re.search(“bdpass\.api\.params\.login_token='(?p\w+)’;”, getapiresphtml) if foundtokenval : tokenval = foundtokenval.group(“tokenval”) print(tokenval) staticpage = “http://zhixin.baidu.com/jump/index?module=onesite” baidumainloginurl = “https://passport.baidu.com/v2/api/?login”

postdict = { ‘charset’:”utf-8″, ‘token’:tokenval, ‘isphone’:”false”, ‘index’:”0″, ‘staticpage’: staticpage, ‘logintype’: “1”, ‘tpl’: “mn”, ‘callback’: “parent.bd__pcbs__n1a3bg”, ‘username’:”*****”, #用户名 ‘password’:”*****”, #密码 ‘mem_pass’:”on”, “apiver”:”v3″, “logintype”:”basiclogin” } postdata = urllib.parse.urlencode(postdict); postdata = postdata.encode(‘utf-8’) resp3=opener.open(baidumainloginurl,data=postdata) for c in cj: print(c.name,”=”*6,c.value) if __name__==”__main__”: print(“=”*10,”开始”) bd=baidu() bd.login()

Posted in 未分类

发表评论