python模擬登錄百度代碼分享(獲取百度貼吧等級)
# -*- coding: utf8 -*-
'''
Created on 2013-12-19
@author: good-temper
'''
import urllib2
import urllib
import cookielib
import re
import bs4
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');
#構造登錄請求參數(shù),該請求數(shù)據(jù)是通過抓包獲得,對應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);
#發(fā)送登錄請求
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/f/like/mylike?v=1387441831248'
content = urllib2.urlopen(teibaUrl).read();
content = content.decode('gbk').encode('utf8');
print content;
#解析數(shù)據(jù),用的BeautifulSoup4,感覺沒有jsoup用的爽
soup = bs4.BeautifulSoup(content);
list = soup.findAll('tr');
list = list[1:len(list)];
careTeibalist = [];
print '貼吧鏈接\t吧名\t等級';
for elem in list:
soup1 = bs4.BeautifulSoup(str(elem));
print 'http://tieba.baidu.com/'+soup1.find('a')['href']+'\t'+soup1.find('a')['title']+'\t'+soup1.find('a',{'class','like_badge'})['title'];
相關文章
使用Python腳本在Linux下實現(xiàn)部分Bash Shell的教程
這篇文章主要介紹了使用Python腳本在Linux下實現(xiàn)部分Bash Shell的教程,包括一些簡單的輸入輸出和郵件功能,需要的朋友可以參考下2015-04-04如何在Python?中使用?Luhn?算法驗證數(shù)字
Luhn 算法驗證器有助于檢查合法數(shù)字并將其與不正確或拼寫錯誤的輸入分開,這篇文章主要介紹了在Python中使用Luhn算法驗證數(shù)字,需要的朋友可以參考下2023-06-06利用Python產(chǎn)生加密表和解密表的實現(xiàn)方法
這篇文章主要介紹了利用Python產(chǎn)生加密表和解密表的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10Python實現(xiàn)的ftp服務器功能詳解【附源碼下載】
這篇文章主要介紹了Python實現(xiàn)的ftp服務器功能,結合實例形式分析了Python構建ftp服務器功能的相關設置、實現(xiàn)技巧與操作注意事項,并附帶源碼供讀者下載參考,需要的朋友可以參考下2019-06-06python將天數(shù)轉(zhuǎn)換為日期字符串的方法實例
這篇文章主要給大家介紹了關于python將天數(shù)轉(zhuǎn)換為日期字符串的相關資料,以及將將字符串的時間轉(zhuǎn)換為時間戳的實例代碼,需要的朋友可以參考下2022-01-01Tensorflow tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積的
這篇文章主要介紹了Tensorflow tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04