Skip to content Skip to sidebar Skip to footer

Python: How To Use Chrome Cookies In Requests

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 this; How

Solution 1:

Toms answer has worked very well for me and was even the only way for me to work in Windows 7 to crawl through sites that require a login. However, with Windows 10 and the Chrome 80 cookie handling (SameSite Cookies) there seems to be a new encryption - the cookie delivered by the "get_cookies" method had all empty values (Python 3).

What worked for me now was browser_cookie3 (fork of browsercookie, was updated just some days ago to work with Chrome 80). I used this with request and selenium.

Install in an elevated prompt with

pip3 install browser-cookie3

Usage with request:

importbrowser_cookie3cookies= browser_cookie3.chrome(domain_name='.google.com')
response = requests.get('http://www.google.com', verify=False, headers=headers, cookies=cookies, timeout=3)

Exchange google.com with the domain of the cookie you need. And be sure to include the timeout parameter or your script may freeze. headers is just an object with all your headers, e.g.

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36", 
    "Accept-Encoding":"gzip, deflate", 
    "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
    "DNT":"1",
    "Connection":"close", 
    "Upgrade-Insecure-Requests":"1"
}

or something like that.

Usage with selenium:

import browser_cookie3
driver = webdriver.Chrome('./chromedriver')
cookies = browser_cookie3.chrome(domain_name='.google.com')
for c in cookies:
    cookie = {'domain': c.domain, 'name': c.name, 'value': c.value, 'secure': c.secure andTrueorFalse}
    driver.add_cookie(cookie)
driver.get('http://www.google.com')

./chromedriver is where my chromedriver.exe lies.

Solution 2:

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')

Post a Comment for "Python: How To Use Chrome Cookies In Requests"