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

相关文章
相关标签/搜索