pycookiecheat

repository·master·Indexed 21 days ago

https://github.com/n8henrie/pycookiecheat

A tool and Python library for borrowing authenticated cookies from browser sessions (including Chrome, Firefox, Brave, Chromium, Slack, and Vivaldi) for use in Python scripts, web automation, and scraping. It provides a command-line interface and a public API featuring functions like get_cookies(), chrome_cookies(), and firefox_cookies() to retrieve decrypted cookies as dictionaries or Cookie objects.

Tokens
3.6K
Snippets
11
Records
13
Agent score
73%

What's inside pycookiecheat

  1. Specify Firefox profiles

    master

    When using Firefox, you can specify which profile to use via the --firefox-profile CLI flag or by providing the profile directory path.

    If you are using the newer Firefox profile system (introduced around 2025), you may need to query the .sqlite files in the Profile Groups directory to find the correct profile name.

    Example CLI usage:

    $ python -m pycookiecheat --firefox-profile=wsw6frhw.default https://example.com
  2. Troubleshoot Linux keyring issues

    master

    On some Linux distributions (like Ubuntu), Chrome uses alternative keyrings that may require additional system dependencies to decrypt cookies.

    To enable support for these keyrings, install the following system packages:

    sudo apt-get install libsecret-1-dev python-gi python3-gi

    Note: If you are using a virtual environment, you must install it with the --system-site-packages flag to ensure access to these libraries.

    Alternatively, you can run Chrome with these flags to use a simpler storage method:

    • --password-store=basic
    • --use-mock-keychain
  3. Use pycookiecheat as a Python Library

    master

    Import get_cookies and BrowserType to programmatically retrieve decrypted cookies for use in scripts (e.g., with requests).

    By default, get_cookies(url) uses the default Chrome cookie filepath. You can specify an alternate browser using BrowserType or provide a specific path to a cookie file using the cookie_file keyword argument.

    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)
    
    # Using a specific cookie file path
    cookies = get_cookies(url, cookie_file='/abspath/to/cookies')
  4. Reference: pycookiecheat CLI Options

    master

    The following options are available when running pycookiecheat from the command line:

    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
  5. Use pycookiecheat as a Command-Line Tool

    master

    You can run the tool as a Python module or via its standalone console script. By default, it outputs cookies as JSON to stdout, but you can also specify an output file in Netscape Cookie File Format.

    Usage: python -m pycookiecheat [OPTIONS] URL

    $ 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
  6. Retrieve cookies using get_cookies()

    master

    The get_cookies function is the primary entrypoint for retrieving cookies from supported browsers on MacOS or Linux. It can return cookies as a dictionary of values or as a list of Cookie objects.

    Arguments

    • url: The domain from which to retrieve cookies (e.g., https://example.com).
    • browser: A BrowserType enum variant (defaults to BrowserType.CHROME).
    • as_cookies: If True, returns a list[Cookie] instead of a dict.
    • cookie_file: Path to an alternate file to search for cookies.
    • curl_cookie_file: Path where a Netscape-style cookie file will be saved for use with cURL.
    • password: Optional system password (required for some Chromium-based browsers; unused for Firefox).
    • firefox_profile_name: Subdirectory name or glob pattern for the Firefox profile (e.g., ashu3ae.default). If omitted, the default profile is used. Unused for non-Firefox browsers.
    from pycookiecheat import get_cookies, BrowserType
    
    # Get cookies as a dictionary (default)
    cookies_dict = get_cookies("https://example.com", browser=BrowserType.CHROME)
    
    # Get cookies as a list of Cookie objects and save a cURL-compatible file
    cookies_list = get_cookies(
        "https://example.com", 
        browser=BrowserType.FIREFOX, 
        as_cookies=True, 
        curl_cookie_file="cookies_for_curl.txt",
        firefox_profile_name="default-profile"
    )
  7. Retrieve cookies from Firefox using firefox_cookies()

    master

    Use the firefox_cookies function to extract cookies from a Firefox browser profile. It returns a dictionary of cookie names and values for a given URL/domain. If as_cookies is set to True, it returns a list of Cookie objects instead.

    Arguments:

    • url (str): The URL or domain from which to retrieve cookies (e.g., https://github.com).
    • browser (BrowserType): The browser type (defaults to BrowserType.FIREFOX).
    • as_cookies (bool): If True, returns list[Cookie]. If False, returns dict[str, str] mapping names to values.
    • cookie_file (str | Path, optional): A custom path to a specific cookie file to search.
    • curl_cookie_file (str, optional): A path where the extracted cookies will be saved in a format compatible with cURL.
    • profile_name (str, optional): A subdirectory name or glob pattern for the Firefox profile (e.g., ashu3ae.default). If omitted, the function attempts to find the default profile via profiles.ini.
    from pycookiecheat import firefox_cookies
    
    # Get a dictionary of cookies
    cookies = firefox_cookies("https://github.com")
    print(cookies)
    # Output example: {'logged_in': 'yes', 'user_session': 'n3tZzN45P56Ovg5MB'}
    
    # Get a list of Cookie objects
    cookies_list = firefox_cookies("https://github.com", as_cookies=True)
    
    # Save cookies to a cURL compatible file
    firefox_cookies("https://github.com", curl_cookie_file="cookies.txt")
    
    # Use a specific profile
    firefox_cookies("https://github.com", profile_name="my-profile-name")
  8. Extract cookies using the pycookiecheat public API

    master

    The pycookiecheat package provides a high-level interface for extracting cookies from Chrome and Firefox browsers. You can use the specialized functions chrome_cookies() or firefox_cookies() for direct access, or the generic get_cookies() function which accepts a BrowserType to determine the source.

    from pycookiecheat import chrome_cookies, firefox_cookies, get_cookies, BrowserType
    
    # Option 1: Use browser-specific functions
    chrome_data = chrome_cookies()
    firefox_data = firefox_cookies()
    
    # Option 2: Use the generic get_cookies function
    # Note: BrowserType is an enum/class used to specify the browser
    all_cookies = get_cookies(BrowserType.CHROME)
  9. Reference pycookiecheat CLI arguments

    master

    The following arguments are available for the pycookiecheat command-line interface:

    ArgumentFlagDescription
    url(positional)The target URL to extract cookies for.
    --browser-bThe browser type to use. Defaults to CHROME.
    --firefox-profile-pSubdirectory name or glob pattern of the Firefox profile (e.g., ashu3ae.default). If omitted, the default profile is used. Unused for non-Firefox browsers.
    --output-file-oIf provided, outputs the cookies to this file in Netscape cookie file format. If omitted, outputs JSON to stdout.
    --cookie-file-cSpecify a specific cookie file.
    --verbose-vIncrease logging verbosity. This flag can be repeated to increase levels. Default is logging.ERROR.
    --version-VShow the version of pycookiecheat.
    python -m pycookiecheat https://example.com -b firefox -p ashu3ae.default -o cookies.txt
  10. Supported browser types with BrowserType

    master

    The BrowserType enum defines the recognized browsers. It supports case-insensitive string matching when initializing via the constructor.

    Supported values:

    • BRAVE ("brave")
    • CHROME ("chrome")
    • CHROMIUM ("chromium")
    • FIREFOX ("firefox")
    • SLACK ("slack")
    • VIVALDI ("vivaldi")

    If you provide a string that does not match any of these (case-insensitively), a ValueError will be raised.

    from pycookiecheat import BrowserType
    
    # Case-insensitive matching works
    browser = BrowserType("FiReFoX")  # Returns BrowserType.FIREFOX
    
    # Invalid browsers raise ValueError
    try:
        invalid = BrowserType("edge")
    except ValueError as e:
        print(e)  # 'edge' is not a valid BrowserType