The code is as follows:
#-*-coding:utf-8-*-
'''
Created on January 10, 2014
@author: Hhdys
'''
Import Urllib.request,http.cookiejar,re
Class 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&class=login&tpl=mn&tangram=true"
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, "start")
Bd=baidu ()
Bd.login ()