Python使用cookielib模塊操作cookie的實(shí)例教程
cookielib是一個(gè)自動(dòng)處理cookies的模塊,如果我們?cè)谑褂门老x等技術(shù)的時(shí)候需要保存cookie,那么cookielib會(huì)讓你事半功倍!他最常見的搭檔模塊就是python下的urllib和request。
核心類
1.Cookie
該類實(shí)現(xiàn)了Netscape and RFC 2965 cookies定義的cookie標(biāo)準(zhǔn),基本可以理解為某一條cookie數(shù)據(jù)。
部分代碼如下,很多屬性是不是很眼熟?
self.domain_initial_dot = domain_initial_dot self.path = path self.path_specified = path_specified self.secure = secure self.expires = expires self.discard = discard self.comment = comment self.comment_url = comment_url self.rfc2109 = rfc2109
2.CookiePolicy
該類的主要功能是收發(fā)cookie,即確保正確的cookie發(fā)往對(duì)應(yīng)的域名,反之一樣。
3.DefaultCookiePolicy
該類實(shí)現(xiàn)了CookiePolicy的接口。
4.CookieJar
CookieJar是cookie的集合,可以包含有很多Cookie類,是我們的主要操作對(duì)象。里面有一系列的方法可以支持更加細(xì)致的操作!
5.FileCookieJar
該類繼承自CookieJar,CookieJar只是在內(nèi)存中完成自己的生命周期,F(xiàn)ileCookieJar的子類能夠?qū)崿F(xiàn)數(shù)據(jù)持久化,定義了save、load、revert三個(gè)接口。
6.MozillaCookieJar & LWPCookieJar
兩個(gè)實(shí)現(xiàn)類,繼承關(guān)系如下:
實(shí)例:登錄人人網(wǎng)
在firefox下使用httpFox插件來查到人人網(wǎng)的登錄時(shí)需要POST的地址是http://www.renren.com/ajaxLogin
而且查看到需要POST的DATA有email和password
python通過cookielib來處理cookie,以下是簡(jiǎn)單的代碼
>>> import urllib >>> import urllib2,cookielib >>> login_page = "http://www.renren.com/ajaxLogin" >>> cj = cookielib.CookieJar() >>> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) >>> opener.add_handler = [('User-agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')] >>> data = urllib.urlencode({"email":'username',"password":'password'}) >>> opener.open(login_page,data) <addinfourl at 53653216 whose fp = <socket._fileobject object at 0x03307B70>> >>> if cj: ... for index,cookie in enumerate(cj): ... print index,':',cookie ... 0 : <Cookie _de=90D533AE20EB964CA96710977F452897 for .renren.com/> 1 : <Cookie anonymid=hlehtdzg-8359yw for .renren.com/> 2 : <Cookie first_login_flag=1 for .renren.com/> 3 : <Cookie id=224967207 for .renren.com/> 4 : <Cookie loginfrom=null for .renren.com/> 5 : <Cookie p=9beb60859c004bcaf0a32ff2c973473d7 for .renren.com/> 6 : <Cookie societyguester=86b6a6006002ab6316f708521ab50bfc7 for .renren.com/> 7 : <Cookie t=86b6a6006002ab6316f708521ab50bfc7 for .renren.com/> 8 : <Cookie xnsid=fa53da51 for .renren.com/> 9 : <Cookie t=30af9ffe774f4d6f242e92da1ccd6670 for .renren.com/xtalk/> 10 : <Cookie feedType=224967207_hot for .www.renren.com/> 11 : <Cookie JSESSIONID=abc3IP9kEhTExblxcRfeu for www.renren.com/> >>>
可以和firebug或者h(yuǎn)ttpFox中得到的cookie進(jìn)行對(duì)比,值可能不一致,但key基本上是一致的,你每次登錄應(yīng)該都不一致
我也嘗試過使用fidder模擬發(fā)送沒有cookie的POST數(shù)據(jù),但是沒有得到想要的返回值
而加上cookie信息以后就可以正常的跳轉(zhuǎn)到自己的主頁了
好了,基本上了解了python中使用cookie來發(fā)送登錄信息,現(xiàn)在我們來寫一個(gè)小腳本來登錄自己人人網(wǎng)。
#encoding=utf-8 import urllib2 import urllib import cookielib def renrenBrower(url,user,password): login_page = "http://www.renren.com/ajaxLogin" try: cj = cookielib.CookieJar() opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.addheaders = [('User-agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')] data = urllib.urlencode({"email":user,"password":password}) opener.open(login_page,data) op=opener.open(url) data= op.read() return data except Exception,e: print str(e) print renrenBrower("http://www.renren.com/home","用戶名","密碼")
這樣就可以將自己首頁的信息顯示出來了,其實(shí)在登錄完以后,還可以接著寫腳本來獲取自己想要的信息,如朋友的新鮮事等,這里就不作過多說明了~
- Python get獲取頁面cookie代碼實(shí)例
- Python Selenium Cookie 繞過驗(yàn)證碼實(shí)現(xiàn)登錄示例代碼
- python爬蟲中g(shù)et和post方法介紹以及cookie作用
- Python爬蟲番外篇之Cookie和Session詳解
- 利用selenium 3.7和python3添加cookie模擬登陸的實(shí)現(xiàn)
- Python HTTP客戶端自定義Cookie實(shí)現(xiàn)實(shí)例
- python模擬登錄并且保持cookie的方法詳解
- Python爬蟲利用cookie實(shí)現(xiàn)模擬登陸實(shí)例詳解
- qpython3 讀取安卓lastpass Cookies
- python3實(shí)現(xiàn)讀取chrome瀏覽器cookie
- 玩轉(zhuǎn)python爬蟲之cookie使用方法
- Python中urllib+urllib2+cookielib模塊編寫爬蟲實(shí)戰(zhàn)
- Python3中關(guān)于cookie的創(chuàng)建與保存
相關(guān)文章
關(guān)于pytest結(jié)合csv模塊實(shí)現(xiàn)csv格式的數(shù)據(jù)驅(qū)動(dòng)問題
這篇文章主要介紹了pytest結(jié)合csv模塊實(shí)現(xiàn)csv格式的數(shù)據(jù)驅(qū)動(dòng),使用python中的csv模塊來處理csv文件,結(jié)合pygtest的參數(shù)化處理方式來實(shí)現(xiàn)ddt,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-05-05使用Python連接MySQL數(shù)據(jù)庫(kù)進(jìn)行編程的步驟詳解
Python數(shù)據(jù)庫(kù)編程可以使用多種模塊與API,例如SQLite、MySQL、PostgreSQL等,本教程將重點(diǎn)介紹使用Python連接MySQL數(shù)據(jù)庫(kù)進(jìn)行編程,需要的朋友可以參考下2023-06-06Python統(tǒng)計(jì)可散列的對(duì)象之容器Counter詳解
Counter是一個(gè)容器,可以跟蹤等效值增加的次數(shù).這個(gè)類可以用來實(shí)現(xiàn)其他語言中常用包或多集合數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)的算法.本篇文章非常詳細(xì)的介紹了容器Counter的使用方式,需要的朋友可以參考下2021-05-05python讀文件保存到字典,修改字典并寫入新文件的實(shí)例
下面小編就為大家分享一篇python讀文件保存到字典,修改字典并寫入新文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04