Cookie-Pool
一個強(qiáng)大的Cookie池項(xiàng)目阅懦,超乎你的想象
1 綜述
隨著大型網(wǎng)站反扒機(jī)制的增強(qiáng)和二,使用cookie登陸已經(jīng)成為一種最高效的方式,為此打造一款超強(qiáng)cookie池項(xiàng)目
基于tornado網(wǎng)絡(luò)框架耳胎,綜合了selenium惯吕、requests、Session怕午、scrapy废登、cookie字符串、瀏覽器cookie
把六中來源的cookie統(tǒng)一為一種格式郁惜,并通過tornado開發(fā)的控制臺堡距,來提供cookie檢測設(shè)置和可視化
本項(xiàng)目分為cookie獲取部分、存儲部分兆蕉、可視與中控部分羽戒、預(yù)留自動登錄獲取部分
2 文件目錄結(jié)構(gòu)說明
2.1 db
peeweetools:封裝了操作sqlite的基本方法和函數(shù)
redistools: 封裝了redis的基本方法和函數(shù)
2.2 handle
getcookie:封裝了從本地瀏覽器、requests虎韵、session易稠、webdriver、cookie字符串中獲取cookie的方法
interface:封裝了協(xié)調(diào)調(diào)用sqlite包蓝、redis的方法缩多,是在peeweetools、redistools的基礎(chǔ)上再次封裝主要實(shí)現(xiàn)存放cookie和獲取cookie的函數(shù) testcookie:定義了用于測試cookie的類
2.4 static
靜態(tài)文件目錄养晋,存放sqlite
2.5 template
可視化的html模板文件,用于tornado渲染
2.6 setting.py
配置文件
2.7 web.py
主程序文件梁钾,實(shí)現(xiàn)調(diào)度和網(wǎng)頁操作绳泉、后臺操作的相關(guān)接口
2.8 requirements.txt
項(xiàng)目涉及到的Python包,使用pip install -r requirements.pip命令自動安裝
安裝browsercookie失敗的話請使用anaconda Python解釋器自帶了這個包
3 架構(gòu)說明
用兩種方式與用戶交互:1.條用web接口 2.調(diào)用內(nèi)部方法
selenium姆泻、requests零酪、Session冒嫡、scrapy的cookie存放目前只能通過調(diào)用內(nèi)部方法
獲取Chrome瀏覽器cookie,解析cookie字符串可通過在線操作和內(nèi)部方法來實(shí)現(xiàn)
4 前端視圖
4.1 配置檢測方式:selenium四苇、requests
檢測的URL孝凌、打開URL后標(biāo)志性的字段
4.2 配置檢測之后點(diǎn)擊保存,然后可以檢測了
如果沒有配置檢測方式及其字段是不能夠檢測的
4.3 檢測原理:
通過打開我們配置的url地址檢測在url地址中只有登陸后才能出現(xiàn)的字段如:用戶名月腋、賬號名
所以配置的URL最好是登陸后才可見的地址
4.4 多賬號配置
如果是多條cookie蟀架,分別對應(yīng)多個賬號,但是有多個用戶名榆骚,這種方式下我們可以把所有用戶名
以英文;連接起來(如:張三;李四;老王),后臺會以;分割sign字段片拍,在每個cookie頁面都作為關(guān)鍵詞檢測
如果含有其中一個那么就是可用的cooker,否則就是失效的cookie妓肢。
5 Redis捌省、SQLite
5.1 Redis
是使用的本地Redis數(shù)據(jù)庫,在window下下載redis——sever.exe服務(wù)端打開即可
也可在setting.py文件中修改REDIS_URL地址為遠(yuǎn)程Redis地址
5.2 SQL
本項(xiàng)目使用的是SQLite數(shù)據(jù)庫碉钠,也可換成mysql
將db/peeweetools.py文件中的
blog = PooledSqliteDatabase('static/cookies.db')修改為PooledMySQLDatabase
具體配置看文檔
6 使用方法
6.1 啟動
在環(huán)境配置好的前提下纲缓,運(yùn)行web.py文件,即可啟動
6.2 存放cookie
6.2.1 存放selenium的cookie
<pre class="public-DraftStyleDefault-pre" data-offset-key="82lo4-0-0" style="box-sizing: inherit; margin: 0px; padding: calc(0.888889em); font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="7r1qd" data-offset-key="82lo4-0-0" style="box-sizing: inherit; margin: 0px; font-weight: normal !important; padding: 0px; font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
from selenium import webdriver
from handle.Interface import put_cookie
url = "http://www.baidu.com'
driver = webdriver.Chrome()
driver.get(url)
data = driver.get_cookies()
put_cookie(url, data)
</pre>
</pre>
此時selenium的cookie就放到了Redis喊废,在redis中的key是cookies:domain格式
6.2.2 存放本地Chrome中的cookie
運(yùn)行web.py之后打開http://localhost:端口/
在獲取cookie框中填入網(wǎng)址
或者通過調(diào)用內(nèi)部方法
<pre class="public-DraftStyleDefault-pre" data-offset-key="d9635-0-0" style="box-sizing: inherit; margin: 0px; padding: calc(0.888889em); font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="7r1qd" data-offset-key="d9635-0-0" style="box-sizing: inherit; margin: 0px; font-weight: normal !important; padding: 0px; font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
from handle.getcookie import get_chrome_cookie
from handle.Interface import put_cookie
url = "http://www.taobao.com"
cookie_list = get_chrome_cookie(url) # 返回淘寶的cookie列表
put_cookie(url, cookie_list)
</pre>
</pre>
6.2.3 存放requests/Session的cookie
向get_reque_session_cookie函數(shù)中傳入response對象將解析cookie
<pre class="public-DraftStyleDefault-pre" data-offset-key="20erp-0-0" style="box-sizing: inherit; margin: 0px; padding: calc(0.888889em); font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="7r1qd" data-offset-key="20erp-0-0" style="box-sizing: inherit; margin: 0px; font-weight: normal !important; padding: 0px; font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
import requests
from requests import Session
from handle.Interface import put_cookie
from handle.getcookie import get_reque_session_cookie
url = "http://www.taobao.com"
response = requests.get(url)
session = Session.get(url)
cookies = get_reque_session_cookie(response)
cookies = get_reque_session_cookie(session)
put_cookie(url, cookies)
</pre>
</pre>
6.2.4 存放scrapy的cookie
使用方法大致同獲取requests/Sessioncookie的方法一致
將scrapy的response對象傳入get_scrapy_cookie函數(shù)
6.2.5 存放cookie字符串
<pre class="public-DraftStyleDefault-pre" data-offset-key="8i9c6-0-0" style="box-sizing: inherit; margin: 0px; padding: calc(0.888889em); font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="7r1qd" data-offset-key="8i9c6-0-0" style="box-sizing: inherit; margin: 0px; font-weight: normal !important; padding: 0px; font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
from handle.Interface import put_cookie
from handle.getcookie import get_text_cookie
url = "http://www.baidu.com"
str = """REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1;
SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; to_lang_often=%5B%7B%22value%22
%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%2
2zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D; from_lang_often=%5B%7B%2
2value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%2
2%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; Hm_lvt_64ecd82404c5
1e03dc91cb9e8c025574=1546396967,1548035203; BAIDUID=E6BB8BCF4B51CC6A516F391
20FB9580F:FG=1; PSTM=1548043541; BIDUPSID=A4BC9C784FA26726086F330B96C55BA1;
ZD_ENTRY=baidu; delPer=0; locale=zh; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598;
Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1548057039; PSINO=3; BDRCVFR[qQNS-
g6pS-_]=mbxnW11j9Dfmh7GuZR8mvqV; H_PS_PSSID=26523_1456_21118_28329_28132_26
350_28266_27245"""
cookies = get_text_cookie(url, str)
put_cookie(url, cookies)
</pre>
</pre>
或者在html頁面操作:
6.3 獲取cookie
6.3.1 通過接口獲取cookie
<pre class="public-DraftStyleDefault-pre" data-offset-key="fkvs2-0-0" style="box-sizing: inherit; margin: 0px; padding: calc(0.888889em); font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="7r1qd" data-offset-key="fkvs2-0-0" style="box-sizing: inherit; margin: 0px; font-weight: normal !important; padding: 0px; font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
import requests
url1 = 'http://localhost:端口號/random?url=網(wǎng)址' # 隨機(jī)獲取一條該域名的cookie
url2 = 'http://localhost:端口號/all?url=網(wǎng)址' # 返回該域名的所有cookie
cookies = requests.get(url1)
</pre>
</pre>
6.3.2 通過內(nèi)部方法獲取cookie
<pre class="public-DraftStyleDefault-pre" data-offset-key="5is5l-0-0" style="box-sizing: inherit; margin: 0px; padding: calc(0.888889em); font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="7r1qd" data-offset-key="5is5l-0-0" style="box-sizing: inherit; margin: 0px; font-weight: normal !important; padding: 0px; font-size: 0.9em; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
from handle.Interface import get_cookie
url = "http://www.taobao.com"
cookies = get_cookie(url) # 返回所有cookie
</pre>
</pre>
6.4 cookie的檢測
6.4.1 配置檢測信息
在檢測之前需要配置相關(guān)的信息
TEST_TYPE:檢測類型祝高,是通過requests打開配置的URL還是通過selenium打開
TEST_URL:含有驗(yàn)證信息的頁面地址如:登錄頁、個人資料頁面
TEST_SIGN:鑒別有效cookie的字段如用戶名等,多個cookie的不同字段用英;連接起來
三個字段必須配置操禀,不配置不能保存褂策;配置后點(diǎn)擊保存,然后再點(diǎn)擊檢測颓屑。
項(xiàng)目github地址,覺得不錯就給個星星吧