python 实现登陆百度,欢迎指正

import urllib2,urllib,re,os
import sqlite3,cookielib,time
def 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')

标签: <无>

代码片段(1)

[代码] [Python]代码

01 import urllib2,urllib,re,os
02 import sqlite3,cookielib,time
03 def LoginBaiDu(self,user,pwd):
04          
05         #设置
06         cookie = cookielib.CookieJar()
07         cookieProc = urllib2.HTTPCookieProcessor(cookie)
08         opener = urllib2.build_opener(cookieProc)
09         urllib2.install_opener(opener)
10  
11         #请求
12         header = {'User-Agent':'Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2'}
13         post = {
14             'username':user,
15             'password':pwd,
16             'tpl':'mn',
17             'u':'http://www.baidu.com/',
18             'psp_tt':,
19             'mem_pass':'on'
20             }
21         post = urllib.urlencode(post)
22          
23         req = urllib2.Request(
24             url='https://passport.baidu.com/?login',
25             data=post,
26             headers = header
27             )
28         res = urllib2.urlopen(req).read(500)
29          
30         if 'passCookie' in res:
31             flag = True
32          
33         else:
34             flag = 'Login Fail:%s'%user
35              
36         return flag
37         print (flag)
38          
39     #sb = SpiderBaiDu()
40 LoginBaiDu('ggght','123456')

你可能感兴趣的:(python 实现登陆百度,欢迎指正)