Python模擬百度登錄實(shí)例詳解
最近公司產(chǎn)品和百度貼吧合作搞活動,為了增加人氣,打算做個(gè)自動簽到的小程序。這個(gè)是測試登錄的代碼,寫的比較隨意,僅實(shí)現(xiàn)了登錄并讀取關(guān)注貼吧列表,下邊的就比較簡單。
百度登錄還是有點(diǎn)麻煩的,由于用的ssl,所以要先獲取token,然后再登錄,這個(gè)用finddle2分析下,還是比較好解決的。
# -*- coding: utf8 -*-
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 也可以用這個(gè)
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';
#設(shè)置用戶名、密碼
username = '';
password = '';
#設(shè)置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');
#構(gòu)造登錄請求參數(shù),該請求數(shù)據(jù)是通過抓包獲得,對應(yīng)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);
#查看貼吧個(gè)人主頁 ,測試是否登陸成功,由于cookie自動管理,這里處理起來方便很多
#http://tieba.baidu.com/home/main?un=XXXX&fr=index 這個(gè)是貼吧個(gè)人主頁,各項(xiàng)信息都可以在此找到鏈接
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'];
關(guān)于python模擬百度登錄相關(guān)知識就給大家介紹這么多,希望大家喜歡。
- Python3 適合初學(xué)者學(xué)習(xí)的銀行賬戶登錄系統(tǒng)實(shí)例
- 基于Python實(shí)現(xiàn)一個(gè)簡單的銀行轉(zhuǎn)賬操作
- python操作MySQL 模擬簡單銀行轉(zhuǎn)賬操作
- Python操作MySQL模擬銀行轉(zhuǎn)賬
- Python基于Logistic回歸建模計(jì)算某銀行在降低貸款拖欠率的數(shù)據(jù)示例
- Python實(shí)現(xiàn)模擬登錄及表單提交的方法
- python實(shí)現(xiàn)網(wǎng)站的模擬登錄
- python模擬登錄并且保持cookie的方法詳解
- Python模擬登錄的多種方法(四種)
- Python實(shí)現(xiàn)的銀行系統(tǒng)模擬程序完整案例
相關(guān)文章
python scp 批量同步文件的實(shí)現(xiàn)方法
今天小編就為大家分享一篇python scp 批量同步文件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python爬蟲實(shí)現(xiàn)抓取電影網(wǎng)站信息并入庫
本文主要介紹了利用Python爬蟲實(shí)現(xiàn)抓取電影網(wǎng)站信息的功能,并將抓取到的信息入庫。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-02-02
python實(shí)現(xiàn)Oracle查詢分組的方法示例
這篇文章主要介紹了python實(shí)現(xiàn)Oracle查詢分組的方法,結(jié)合實(shí)例形式分析了python使用group by子句及having子句實(shí)現(xiàn)Oracle查詢分組的相關(guān)操作技巧,需要的朋友可以參考下2020-04-04
win10系統(tǒng)下Anaconda3安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了win10系統(tǒng)下Anaconda3安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Scrapy-Redis結(jié)合POST請求獲取數(shù)據(jù)的方法示例
這篇文章主要給大家介紹了關(guān)于Scrapy-Redis結(jié)合POST請求獲取數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Scrapy-Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python處理和解析CLIXML數(shù)據(jù)的方法
在使用Windows的Windows Remote Management (WinRM)服務(wù)與PowerShell交互時(shí),經(jīng)常會遇到CLIXML(即CLI XML)格式的數(shù)據(jù),本文將介紹如何在Python中處理和解析CLIXML數(shù)據(jù),并提供一種方法來從數(shù)據(jù)中提取有效信息,需要的朋友可以參考下2024-04-04

