好几年前我在做一些自动化的脚本时,脑子里也闪过这样的想法:能不能直接把浏览器的cookies取出来用呢?
直到昨天看到代码《python模拟发送动弹》,想起来当年我也曾经有类似的想法没能完成,那就优先拿这个练手,之后的代码也会用这个功能。
直接从浏览器中取出cookies,有以下好处和用途:
1、不需要配置用户密码,直接读出浏览器中cookies就得到一样的身份,用来完成各种自动化操作。
2、部分网站登录会更新Session,会导致之前成功登录的Session失效,与浏览器使用相同的Session,不用进行登录操作,不会互相挤下线。
3、全是废话,我不想写了,行吗?
使用到软件的sqlite3的图形管理工具有:
SQLiteDatabaseBrowserPortable http://sqlitebrowser.org/
sqlitespy http://www.yunqa.de/delphi/products/sqlitespy/index
使用到的python库有:
sqlite3 python标准库,不需要下载安装
pywin32 pywin32 windows的API库,让python可以调用各种各样的windows API,代码中用到的win32crypt就是属于pywin32库的一部分。
建议手动下载对应版本pywin32安装 https://sourceforge.net/projects/pywin32/?source=directory
requests requests是一个相对比较简单易用的http库,用来代替urllib23之类的标准库,使用命令安装pip install requests
看代码: | """ | | python3从chrome浏览器读取cookie | | get cookie from chrome | | 2016年5月26日 19:50:38 codegay | | | | 参考资料: | | | | 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 | | """ | | 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 | | | | | | | | | | | | | | url='http://my.oschina.net/' | | | | httphead={'User-Agent':'Safari/537.36',} | | | | | | r=requests.get(url,headers=httphead,cookies=getcookiefromchrome('.oschina.net'),allow_redirects=1) | | print(r.text)COPY |
参考资料:
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 |