百度賬號登陸

#! /user/bin/env python
#encoding=utf-8
__author__ = 'chw'
import urllib2
import urllib
import cookielib
import re
import chardet
URL_BAIDU_INDEX = u'http://www.baidu.com/'
#https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true 也可以用這個
URL_BAIDU_TOKEN = 'https://passport.baidu.com/v2/api/?getapi&tpl=pp&apiver=v3&class=login'
URL_BAIDU_LOGIN = 'https://passport.baidu.com/v2/api/?login'
#設置用戶名、密碼
username = '********'
password = '********'
#設置cookie,這裏cookiejar可自動管理,無需手動指定
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
reqReturn = urllib2.urlopen(URL_BAIDU_INDEX)
#獲取token,
tokenReturn = urllib2.urlopen(URL_BAIDU_TOKEN)
matchVal = re.search(u'"token" : "(?P<tokenVal>.*?)"',tokenReturn.read())
tokenVal = matchVal.group('tokenVal')
#構造登錄請求參數,該請求數據是通過抓包獲得,對應https://passport.baidu.com/v2/api/?login請求
postData = {
'username' : username,
'password' : password,
'u' : 'https://passport.baidu.com/',
'tpl' : 'pp',
'token' : tokenVal,
'staticpage' : 'https://passport.baidu.com/static/passpc-account/html/v3Jump.html',
'isPhone' : 'false',
'charset' : 'UTF-8',
'callback' : 'parent.bd__pcbs__ra48vi'
};
postData = urllib.urlencode(postData)
#發送登錄請求
loginRequest = urllib2.Request(URL_BAIDU_LOGIN,postData)
loginRequest.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
loginRequest.add_header('Accept-Encoding','gzip,deflate,sdch');
loginRequest.add_header('Accept-Language','zh-CN,zh;q=0.8');
loginRequest.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36');
loginRequest.add_header('Content-Type','application/x-www-form-urlencoded');
sendPost = urllib2.urlopen(loginRequest);
#查看貼吧個人主頁 ,測試是否登陸成功,由於cookie自動管理,這裏處理起來方便很多
#http://tieba.baidu.com/home/main?un=XXXX&fr=index 這個是貼吧個人主頁,各項信息都可以在此找到鏈接
teibaUrl = 'http://tieba.baidu.com/home/main?id=5890636877d0d0caa4d3dad1d49131&fr=itb'
content = urllib2.urlopen(teibaUrl).read();
print chardet.detect(content)
print content
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章