1.jpg
2.jpg

3.jpg
4.jpg
5.jpg

#coding:utf-8

import urllib
import urllib2
import cookielib

#百度
login_url = '//passport.baidu.com/v2/?login'
post_url = '//passport.baidu.com/v2/api/?login'

#用cookielib模块创建一个对象,再用urlllib2模块创建一个cookie的handler
cookie = cookielib.CookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cookie)

#有些网站反爬虫,这里用headers把程序伪装成浏览器
header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36'}

#登录需要提交的表单
#百度
pstdata = {'staticpage':'//passport.baidu.com/static/passpc-account/html/v3Jump.html',
           'charset':'UTF-8',
           'token':'XXXXXXXXXXXXXXXXXXXXXXXX',
           'tpl':'pp',
           'subpro':'',
           'apiver':'v3',
           'tt':'XXXXXXXXXXXX',
           'codestring':'',
           'safeflg':'0',
           'u':'//passport.baidu.com/',
           'isPhone':'',
           'detect':'1',
           'gid':'XXXXXXXXXXXXXXXXXXXXXXXXX',
           'quick_user':'0',
           'logintype':'basicLogin',
           'logLoginType':'pc_loginBasic',
           'idc':'',
           'loginmerge':'true',
           'username':'XXXXXXXXXXXXXX',
           'password':'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
           'verifycode':'',
           'mem_pass':'on',
           'rsakey':'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
           'crypttype':'12',
           'ppui_logintime':'xxxxxx,
           'callback':'xxxxxxxxxxxxxxx',
         }

data = urllib.urlencode(pstdata) #表单数据编码成url识别的格式
req = urllib2.Request(url=post_url,data=data,headers=header) #伪装成浏览器,访问该页面,并POST表单数据,这里并没有实际访问,只是创建了一个有该功能的对象
opener = urllib2.build_opener(cookie_handler) #绑定handler,创建一个自定义的opener
response = opener.open(req) #请求网页,返回句柄
page = response.read() #读取并返回网页内容

print 'Cookie:',cookie

print(page) #打印到终端显示