Python: How to use Chrome cookies in requests

后端 未结 2 1402
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-09 18:55

I am searching for a method to get my cookies from my chrome browser (default), and use it using requests.

I have ofcourse already searched around, and found for example

2条回答
  •  迷失自我
    2021-02-09 19:38

    I have a good script to read Chrome cookies directly on /Default/Cookies. I think you would work fine.

    import sqlite3
    import sys
    from os import getenv, path
    import os
    from Crypto.Cipher import AES
    from Crypto.Protocol.KDF import PBKDF2
    import keyring
    
    def get_cookies(url, cookiesfile):
    
        def chrome_decrypt(encrypted_value, key=None):
            dec = AES.new(key, AES.MODE_CBC, IV=iv).decrypt(encrypted_value[3:])
            decrypted = dec[:-dec[-1]].decode('utf8')
            return decrypted
    
        cookies = []
        if sys.platform == 'win32':
            import win32crypt
            conn = sqlite3.connect(cookiesfile)
            cursor = conn.cursor()
            cursor.execute(
                'SELECT name, value, encrypted_value FROM cookies WHERE host_key == "' + url + '"')
            for name, value, encrypted_value in cursor.fetchall():
                if value or (encrypted_value[:3] == b'v10'):
                    cookies.append((name, value))
                else:
                    decrypted_value = win32crypt.CryptUnprotectData(
                        encrypted_value, None, None, None, 0)[1].decode('utf-8') or 'ERROR'
                    cookies.append((name, decrypted_value))
    
        elif sys.platform == 'linux':
            my_pass = 'peanuts'.encode('utf8')
            iterations = 1
            key = PBKDF2(my_pass, salt, length, iterations)
            conn = sqlite3.connect(cookiesfile)
            cursor = conn.cursor()
            cursor.execute(
                'SELECT name, value, encrypted_value FROM cookies WHERE host_key == "' + url + '"')
            for name, value, encrypted_value in cursor.fetchall():
                decrypted_tuple = (name, chrome_decrypt(encrypted_value, key=key))
                cookies.append(decrypted_tuple)
        else:
            print('This tool is only supported by linux and Mac')
    
        conn.close()
        return cookies
    
    
    if __name__ == '__main__':
        pass
    else:
        salt = b'saltysalt'
        iv = b' ' * 16
        length = 16
    
    #get_cookies('YOUR URL FROM THE COOKIES', 'YOUR PATH TO THE "/Default/Cookies" DATA')
    

提交回复
热议问题