欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-11-02 20:51:24
复制代码 代码如下:#-*-coding:utf-8-*-'''created on 2014年1月10日 @author: hhdys'''import urllib....

复制代码 代码如下:

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

@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<tokenval>\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()