Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

n8henrie/pycookiecheat

Open more actions menu

Repository files navigation

pycookiecheat

master branch build status

Borrow cookies from your browser's authenticated session for use in Python scripts.

Installation

NB: Use pip and python instead of pip3 and python3 if you're still on Python 2 and using pycookiecheat < v0.4.0. pycookiecheat >= v0.4.0 requires Python 3 and in general will aim to support python versions that are stable and not yet end-of-life: https://devguide.python.org/versions.

  • python3 -m pip install pycookiecheat

Installation notes regarding alternative keyrings on Linux

See #12. Chrome is now using a few different keyrings to store your Chrome Safe Storage password, instead of a hard-coded password. Pycookiecheat doesn't work with most of these so far, and to be honest my enthusiasm for adding support for ones I don't use is limited. However, users have contributed code that seems to work with some of the recent Ubuntu desktops. To get it working, you may have to sudo apt-get install libsecret-1-dev python-gi python3-gi, and if you're installing into a virtualenv (highly recommended), you need to use the --system-site-packages flag to get access to the necessary libraries.

Alternatively, some users have suggested running Chrome with the --password-store=basic or --use-mock-keychain flags.

Development Setup

  1. git clone https://github.com/n8henrie/pycookiecheat.git
  2. cd pycookiecheat
  3. python3 -m venv .venv
  4. ./.venv/bin/python -m pip install -e .[dev]

Usage

As a Command-Line Tool

After installation, the CLI tool can be run as a python module python -m or with a standalone console script:

$ python -m pycookiecheat --help
usage: pycookiecheat [-h] [-b BROWSER] [-p FIREFOX_PROFILE] [-o OUTPUT_FILE]
                     [-v] [-c COOKIE_FILE] [-V] url

Copy cookies from Chrome or Firefox and output as json

positional arguments:
  url

options:
  -h, --help            show this help message and exit
  -b BROWSER, --browser BROWSER
  -p FIREFOX_PROFILE, --firefox-profile FIREFOX_PROFILE
                        Subdirectory name (or glob pattern) of the Firefox
                        profile to search for cookies (e.g.,
                        ashu3ae.default) -- if none given it will find the
                        configured default profile. Unused for non-Firefox
                        browsers
  -o OUTPUT_FILE, --output-file OUTPUT_FILE
                        Output to this file in netscape cookie file format
  -v, --verbose         Increase logging verbosity (may repeat), default is
                        `logging.ERROR`
  -c COOKIE_FILE, --cookie-file COOKIE_FILE
                        Cookie file
  -V, --version         show program's version number and exit

By default it prints the cookies to stdout as JSON but can also output a file in Netscape Cookie File Format.

As a Python Library

from pycookiecheat import BrowserType, get_cookies
import requests

url = 'https://n8henrie.com'

# Uses Chrome's default cookies filepath by default
cookies = get_cookies(url)
r = requests.get(url, cookies=cookies)

# Using an alternate browser
cookies = get_cookies(url, browser=BrowserType.CHROMIUM)

Use the cookie_file keyword-argument to specify a different path to the file containing your cookies: get_cookies(url, cookie_file='/abspath/to/cookies')

You may be able to retrieve cookies for alternative Chromium-based browsers by manually specifying something like "/home/username/.config/BrowserName/Default/Cookies" as your cookie_file.

Features

  • Returns decrypted cookies from Google Chrome, Brave, or Slack, on MacOS or Linux.
  • Optionally outputs cookies to file (thanks to Muntashir Al-Islam!)

FAQ / Troubleshooting

How do I specify Firefox profiles?

The profile argument (--firefox-profile in the CLI) specifies the profile in the Firefox profile directory to load from.

For example, on Linux:

$ ls ~/.mozilla/firefox
'Profile Groups'   profiles.ini   wsw6frhw.default

You can specify --firefox-profile=wsw6frhw.default.

Around 2025 Firefox added a new profile system that coexists with the old one. While the metadata for legacy profiles is in PROFILE_DIR/profiles.ini, to find the path for the new profiles you need to check PROFILE_DIR/Profile\ Groups/*.sqlite:

$ sqlite3 ~/snap/firefox/common/.mozilla/firefox/Profile\ Groups/*.sqlite 'SELECT * FROM profiles;'
1|wsw6frhw.default|Original profile|shopping|default-theme@mozilla.org|rgb(255,255,255)|rgb(28,27,34)

How about Windows?

I don't use Windows or have a PC, so I won't be adding support myself. Feel free to make a PR :)

I get an installation error with the cryptography module on OS X

(pycookiecheat <v0.4.0)

If you're getting this error and using Homebrew, then you need to follow the instructions for Building cryptography on OS X and export LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" and try again.

I get an installation error with the cryptography module on Linux

Please check the official cryptography docs. On some systems (e.g. Ubuntu), you may need to do something like sudo apt-get install build-essential libssl-dev libffi-dev python-dev prior to installing with pip.

How can I use pycookiecheat on KDE-based Linux distros?

On KDE, Chrome defaults to using KDE's own keyring, KWallet. For pycookiecheat to support KWallet the dbus-python package must be installed.

How do I install the (unreleased) master branch with pip?

  • python -m pip install git+https://github.com/n8henrie/pycookiecheat@master

Buy Me a Coffee

☕️

About

Borrow cookies from your browser's authenticated session for use in Python scripts.

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.