python 实现登陆百度,欢迎指正,python指正,[Python]代码im


[Python]代码

import urllib2,urllib,re,osimport sqlite3,cookielib,timedef LoginBaiDu(self,user,pwd):        #设置        cookie = cookielib.CookieJar()        cookieProc = urllib2.HTTPCookieProcessor(cookie)        opener = urllib2.build_opener(cookieProc)        urllib2.install_opener(opener)        #请求        header = {'User-Agent':'Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2'}        post = {            'username':user,            'password':pwd,            'tpl':'mn',            'u':'http://www.baidu.com/',            'psp_tt':0,            'mem_pass':'on'            }        post = urllib.urlencode(post)        req = urllib2.Request(            url='https://passport.baidu.com/?login',            data=post,            headers = header            )        res = urllib2.urlopen(req).read(500)        if 'passCookie' in res:            flag = True        else:            flag = 'Login Fail:%s'%user        return flag        print (flag)    #sb = SpiderBaiDu()LoginBaiDu('ggght','123456')

评论关闭