browser-cookie3
browser-cookie3 is a Python library that loads cookies from various web browsers (Chrome, Firefox, Edge, Safari, Opera, Brave, etc.) into a `cookiejar` object. This enables Python programs, often used for web scraping or automation, to download content as seen in a web browser without needing to re-authenticate. The library is actively maintained, with frequent updates to support new browser versions and address changes in cookie storage mechanisms.
Warnings
- gotcha Browser updates frequently change cookie storage formats, encryption methods, or file locations. These external changes can unexpectedly break `browser-cookie3`'s ability to locate or decrypt cookies for specific browser versions or operating systems. Users may need to wait for library updates to restore compatibility.
- breaking Major browsers (Chrome, Firefox, Safari) are phasing out or severely restricting third-party cookies. While `browser-cookie3` primarily handles first-party cookies, this broader change in the web's cookie landscape might affect expected scraping behavior or introduce new errors if your target websites rely on third-party cookie functionality for authentication or content delivery.
- gotcha Users frequently report browser-specific and OS-specific issues, such as cookie decryption failures for Microsoft Edge or Chrome on certain Windows versions, incorrect Firefox profile detection on Linux, or the library ceasing to function entirely on particular setups. These often manifest as `FileNotFoundError`, decryption errors, or empty cookie jars.
- gotcha On Linux, `dbus-python` and `jeepney` are crucial dependencies for `browser-cookie3` to interact with some browsers. Users often encounter errors if these packages are not correctly installed, especially `dbus-python`, which may require system-level packages (e.g., `python3-dbus` on Debian/Ubuntu) in addition to pip installation.
- breaking A segmentation fault (`Segfault`) has been reported when running `browser-cookie3` with Python 3.14 (specifically, in a free-threaded build). This indicates a potential incompatibility with future Python versions, which might break functionality for users upgrading their Python environment.
Install
-
pip install browser-cookie3
Imports
- browser_cookie3
import browser_cookie3
- chrome
cj = browser_cookie3.chrome()
Quickstart
import browser_cookie3
import requests
import os
def get_cookies_for_requests(browser_function):
"""Gets cookies from a specified browser function and returns a requests.cookies.RequestsCookieJar."""
try:
# The specific browser function (e.g., browser_cookie3.chrome())
# returns a http.cookiejar.CookieJar object.
cj = browser_function(domain_name='example.com') # Filter for a specific domain for efficiency
return cj
except Exception as e:
print(f"Error loading cookies from {browser_function.__name__}: {e}")
return None
# Example: Get cookies from Chrome and use them with requests
# Make sure you are logged into 'example.com' in Chrome before running.
chrome_cookies = get_cookies_for_requests(browser_cookie3.chrome)
if chrome_cookies:
print(f"Successfully loaded {len(list(chrome_cookies))} cookies from Chrome.")
# Use the cookies with the requests library
# Replace 'https://example.com' with the actual URL you want to access
# Ensure the URL matches the domain for which cookies were loaded.
try:
response = requests.get('https://example.com', cookies=chrome_cookies)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
print(f"Successfully fetched page from example.com. Status code: {response.status_code}")
# print(response.text[:500]) # Print first 500 characters of the content
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
else:
print("Could not load cookies from Chrome. Make sure Chrome is running and you are logged in.")
# You can also try other browsers:
# firefox_cookies = get_cookies_for_requests(browser_cookie3.firefox)
# if firefox_cookies:
# print(f"Loaded {len(list(firefox_cookies))} cookies from Firefox.")