常见浏览器获取本地cookie的方法

部分网站无法自动生成或者使用selenium获取cookie(某宝模拟登录会被检测出来,需要ua参数),这时可以尝试在网站中手动登录后,获取浏览器保留在本地中的cookie;

Chrome

import os
import json
import base64
import sqlite3
from win32crypt import CryptUnprotectData
from cryptography.hazmat.primitives.ciphers.aead import AESGCM


# 获取encrypted_key
def get_string(s_local_state):
    with open(s_local_state, 'r', encoding='utf-8') as f:
        s_encrtpted_key = json.load(f)['os_crypt']['encrypted_key']
    return s_encrtpted_key


def pull_the_key(base64_encrypted_key):
    c_encrypted_key_with_header = base64.b64decode(base64_encrypted_key)
    c_encrypted_key = c_encrypted_key_with_header[5:]
    c_key = CryptUnprotectData(c_encrypted_key, None, None, None, 0)[1]
    return c_key


# v10版本解密
def decrypt_string(c_key, c_data):
    c_nonce, c_cipherbytes = c_data[3:15], c_data[15:]
    aesgcm = AESGCM(c_key)
    c_plainbytes = aesgcm.decrypt(c_nonce, c_cipherbytes, None)
    s_plaintext = c_plainbytes.decode('utf-8')
    return s_plaintext


# 获取 Chrome 或 Edge 登录本地cookies
def get_cookie_from_chrome(s_browser_name, s_host):
    """
        s_browser_name: 浏览器名称(Google Chrome, Microsoft Edge)
        s_host: 域名   例如: '.umeng.com'
    """
    # 获取相关浏览器本地cookie路径
    if s_browser_name == 'Google Chrome':
        s_path = r'\Google\Chrome\User Data\Local State'
        s_cookie = r'\Google\Chrome\User Data\Default\Cookies'
    else:
        s_path = r'\Microsoft\Edge\User Data\Local State'
        s_cookie = r'\Microsoft\Edge\User Data\Default\Cookies'
    print('{}浏览器cookies本地路径:{}'.format(s_browser_name, s_cookie))
    s_local_state = os.environ['LOCALAPPDATA'] + s_path
    s_cookie_path = os.environ['LOCALAPPDATA'] + s_cookie
    sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % s_host
    # 获取d_cookie
    with sqlite3.connect(s_cookie_path) as conn:
        p_cursor = conn.cursor()
        p_res = p_cursor.execute(sql).fetchall()
        p_cursor.close()
        key = pull_the_key(get_string(s_local_state))
        d_cookie = dict()
        for s_host_key, s_name, c_encrypted_value in p_res:
            if c_encrypted_value[0:3] == b'v10':
                d_cookie[s_name] = decrypt_string(key, c_encrypted_value)
            else:
                d_cookie[s_name] = CryptUnprotectData(c_encrypted_value)[1].decode()

        return d_cookie

Firefox

import os
import sqlite3


# 获取火狐浏览器cookies本都存储位置
def get_firfox_cookie_path():
    # 火狐浏览器文件路径
    s_cookiepath_common = os.environ['APPDATA'] + r"\Mozilla\Firefox\Profiles"
    # 获取所有文件
    l_folds_arr = os.listdir(s_cookiepath_common)
    # 获取所有文件后缀名
    l_folds_end = [os.path.splitext(s_file)[-1][1:] for s_file in l_folds_arr]
    # 判断是否存在后缀为default-release
    if 'default-release' in l_folds_end:
        cookie_fold_index = l_folds_end.index('default-release')
    else:
        cookie_fold_index = l_folds_end.index('default')
    # 获取cookies文件所在,目录
    cookie_fold = l_folds_arr[cookie_fold_index]
    cookie_path = os.path.join(s_cookiepath_common, cookie_fold)
    return os.path.join(cookie_path, 'cookies.sqlite')


# 获取 狐火浏览器 登录本地cookies
def get_cookie_from_firefox(s_host):
    # 获取cookies所在目录
    cookie_path = get_firfox_cookie_path()
    print('FireFox浏览器cookies本地路径:{}'.format(cookie_path))
    # 获取所需网站的cookies
    sql = "select host,name,value from moz_cookies where host='%s'" % s_host
    # 读取cookies文件内容
    with sqlite3.connect(cookie_path) as conn:
        cur = conn.cursor()
        d_cookie = dict()

        res = cur.execute(sql).fetchall()
        for host_key, name, value in res:
            if name == 'miniDialog':
                continue
            d_cookie[name] = value

        if d_cookie:
            return d_cookie

Microsoft Edge

Edge 获取本地cookie的方法和Chrome一致

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值