python3 讀取chrome瀏覽器cookies

原文連接:https://www.cnblogs.com/gayhub/p/pythongetcookiefromchrome.htmlhtml

好幾年前我在作一些自動化的腳本時,腦子裏也閃過這樣的想法:能不能直接把瀏覽器的cookies取出來用呢?python

直到昨天看到代碼《python模擬發送動彈》,想起來當年我也曾經有相似的想法沒能完成,那就優先拿這個練手,以後的代碼也會用這個功能。linux

直接從瀏覽器中取出cookies,有如下好處和用途: 一、不須要配置用戶密碼,直接讀出瀏覽器中cookies就獲得同樣的身份,用來完成各類自動化操做。 二、部分網站登陸會更新Session,會致使以前成功登陸的Session失效,與瀏覽器使用相同的Session,不用進行登陸操做,不會互相擠下線。 三、全是廢話,我不想寫了,行嗎?web

使用到軟件的sqlite3的圖形管理工具備: SQLiteDatabaseBrowserPortable http://sqlitebrowser.org/ sqlitespy http://www.yunqa.de/delphi/products/sqlitespy/indexsql

使用到的python庫有: sqlite3 python標準庫,不須要下載安裝chrome

pywin32 pywin32是python版的windows API庫,讓python能夠調用各類各樣的windows API,代碼中用到的win32crypt就是屬於pywin32庫的一部分。 建議手動下載對應版本pywin32安裝 https://sourceforge.net/projects/pywin32/?source=directory數據庫

requests requests是一個相對比較簡單易用的http庫,用來代替urllib23之類的標準庫,使用命令安裝pip install requestssegmentfault

看代碼:windows

""" python3從chrome瀏覽器讀取cookie get cookie from chrome 2016年5月26日 19:50:38 codegay """
import os import sqlite3 import requests from win32.win32crypt import CryptUnprotectData def getcookiefromchrome(host='.oschina.net'): cookiepath=os.environ['LOCALAPPDATA']+r"\Google\Chrome\User Data\Default\Cookies" sql="select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu=conn.cursor() cookies={name:CryptUnprotectData(encrypted_value)[1].decode() for host_key,name,encrypted_value in cu.execute(sql).fetchall()} print(cookies) return cookies #運行環境windows 2012 server python3.4 x64 chrome 50 #如下是測試代碼 #getcookiefromchrome() #getcookiefromchrome('.baidu.com')
 url='http://my.oschina.net/' httphead={'User-Agent':'Safari/537.36',} #設置allow_redirects爲真,訪問http://my.oschina.net/ 能夠跟隨跳轉到我的空間
r=requests.get(url,headers=httphead,cookies=getcookiefromchrome('.oschina.net'),allow_redirects=1) print(r.text)

另外:瀏覽器

IE瀏覽器Cookie數據位於:%APPDATA%\Microsoft\Windows\Cookies\ 目錄中的xxx.txt文件 (裏面可能有不少個.txt Cookie文件)
如:C:\Users\yren9\AppData\Roaming\Microsoft\Windows\Cookies\0WQ6YROK.txt

在IE瀏覽器中,IE將各個站點的Cookie分別保存爲一個XXX.txt這樣的純文本文件(文件個數可能不少,但文件大小都較小);而Firefox和Chrome是將全部的Cookie都保存在一個文件中(文件大小較大),該文件的格式爲SQLite3數據庫格式的文件。

Firefox的Cookie數據位於:%APPDATA%\Mozilla\Firefox\Profiles\ 目錄中的xxx.default目錄,名爲cookies.sqlite的文件。
如:C:\Users\jay\AppData\Roaming\Mozilla\Firefox\Profiles\ji4grfex.default\cookies.sqlite
在Firefox中查看cookie, 能夠選擇」工具 > 選項 >」 「隱私 > 顯示cookie」。

Chrome的Cookie數據位於:%LOCALAPPDATA%\Google\Chrome\User Data\Default\ 目錄中,名爲Cookies的文件。
如:C:\Users\jay\AppData\Local\Google\Chrome\User Data\Default\Cookies

在Linux系統上(以Ubuntu 12.04 和 RHEL6.x 爲例)瀏覽器的Cookie
Firefox的Cookie路徑爲:$HOME/.mozilla/firefox/xxxx.default/目錄下的cookie.sqlite文件。

View Code BASH
1234master@jay-linux:~/.mozilla/firefox/tffagwsn.default$ ll cookies.sqlite-rw-r--r-- 1 master master 1572864 Apr 21 16:54 cookies.sqlitemaster@jay-linux:~/.mozilla/firefox/tffagwsn.default$ pwd/home/master/.mozilla/firefox/tffagwsn.default

參考資料:

http://en.wikipedia.org/wiki/HTTP_cookie

http://www.milincorporated.com/a2_cookies.html

http://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data

http://superuser.com/questions/292952/chrome-cookies-folder-in-windows-7

 

原文地址:http://blog.sina.com.cn/s/blog_477071c50102vjai.html

 -------------------------------------------------------------------------分割線-----------------------------------------------------------------------------

get_chrome_cookies(url) 函數, 能夠獲取 Chrome 瀏覽器的 Cookies 信息. 程序在 Windows 下調試經過, 由於 C 盤須要特殊權限來讀寫文件, 所以程序先將 Cookies 數據庫文件拷貝到 D 盤. 該方法用到了第三方庫 win32crypt.

 

import sqlite3

 

import win32crypt

 

import os

 

 

 

def get_chrome_cookies(url):

 

    os.system('copy "C:\\Users\\Liu\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cookies" D:\\python-chrome-cookies')

 

    conn = sqlite3.connect("d:\\python-chrome-cookies")

 

    ret_list = []

 

    ret_dict = {}

 

    for row in conn.execute("select host_key, name, path, value, encrypted_value from cookies"):

 

        if row[0] != url:

 

            continue

 

        ret = win32crypt.CryptUnprotectData(row[4], None, None, None, 0)

 

        ret_list.append((row[1], ret[1]))

 

        ret_dict[row[1]] = ret[1].decode()

 

    conn.close()

 

    os.system('del "D:\\python-chrome-cookies"')

 

    return ret_dict
使用方法: x = requests.get(url, cookies = get_chrome_cookies(domain))
 

參考資料:

python模擬發送動彈

http://www.oschina.net/code/snippet_209614_21944

用Python進行SQLite數據庫操做

http://www.cnblogs.com/yuxc/archive/2011/08/18/2143606.html

encrypted_value解密腳本

http://www.ftium4.com/chrome-cookies-encrypted-value-python.html

利用cookie劫持微博私信

https://segmentfault.com/a/1190000002569850

你所不知道的HostOnly Cookie

https://imququ.com/post/host-only-cookie.html

相關文章
相關標籤/搜索