亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python3實(shí)現(xiàn)讀取chrome瀏覽器cookie

 更新時(shí)間:2016年06月19日 16:59:17   作者:黑暗圣堂武士  
這里給大家分享的是python3讀取chrome瀏覽器的cookie(CryptUnprotectData解密)的代碼,主要思路是讀取到的cookies被封裝成字典,可以直接給requests使用。

好幾年前我在做一些自動(dòng)化的腳本時(shí),腦子里也閃過(guò)這樣的想法:能不能直接把瀏覽器的cookies取出來(lái)用呢?

直到昨天看到代碼《python模擬發(fā)送動(dòng)彈》,想起來(lái)當(dāng)年我也曾經(jīng)有類(lèi)似的想法沒(méi)能完成,那就優(yōu)先拿這個(gè)練手,之后的代碼也會(huì)用這個(gè)功能。

直接從瀏覽器中取出cookies,有以下好處和用途:

1、不需要配置用戶密碼,直接讀出瀏覽器中cookies就得到一樣的身份,用來(lái)完成各種自動(dòng)化操作。

2、部分網(wǎng)站登錄會(huì)更新Session,會(huì)導(dǎo)致之前成功登錄的Session失效,與瀏覽器使用相同的Session,不用進(jìn)行登錄操作,不會(huì)互相擠下線。

3、全是廢話,我不想寫(xiě)了,行嗎?

使用到軟件的sqlite3的圖形管理工具有:

SQLiteDatabaseBrowserPortable http://chabaoo.cn/database/251740.html

sqlitespy http://chabaoo.cn/database/18390.html

使用到的python庫(kù)有:

sqlite3 python標(biāo)準(zhǔn)庫(kù),不需要下載安裝

pywin32 pywin32 windows的API庫(kù),讓python可以調(diào)用各種各樣的windows API,代碼中用到的win32crypt就是屬于pywin32庫(kù)的一部分。 建議手動(dòng)下載對(duì)應(yīng)版本pywin32安裝 http://chabaoo.cn/softs/416136.html http://chabaoo.cn/softs/416131.html

requests requests是一個(gè)相對(duì)比較簡(jiǎn)單易用的http庫(kù),用來(lái)代替urllib23之類(lèi)的標(biāo)準(zhǔn)庫(kù),使用命令安裝pip install requests

看代碼:

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

#運(yùn)行環(huán)境windows 2012 server python3.4 x64 chrome 50
#以下是測(cè)試代碼
#getcookiefromchrome()
#getcookiefromchrome('.baidu.com')

url='http://my.oschina.net/'

httphead={'User-Agent':'Safari/537.36',}

#設(shè)置allow_redirects為真,訪問(wèn)http://my.oschina.net/ 可以跟隨跳轉(zhuǎn)到個(gè)人空間
r=requests.get(url,headers=httphead,cookies=getcookiefromchrome('.oschina.net'),allow_redirects=1)
print(r.text)

相關(guān)文章

最新評(píng)論