获取谷歌浏览器cookie的两种方法
一、读取浏览器的中文件获取,使用sqlite3
import sqlite3
import win32crypt
username = os.environ.get('USERNAME') cookie_file = 'C:/Users/{UserName}/AppData/Local/Google/Chrome/User Data/Default/Cookies'.format(UserName=username) con = sqlite3.connect(cookie_file) cursor = con.cursor() sql = 'SELECT host_key, name, value, encrypted_value FROM cookies WHERE name = "xxxxx" and host_key="xxxxx";'try: if cursor.execute(sql): for en_value in cursor: pwdHash = en_value[3] if pwdHash: ret = win32crypt.CryptUnprotectData(pwdHash, None, None, None, 0)#解密 a = bytes.decode(ret[1])except Exception as e: print(e)
二、使用selenium
from selenium import webdriver driver = webdriver.Chrome() # 打开谷歌浏览器, driver.get('https://baidu.com') cookie = driver.get_cookies() driver.quit()