Skip to main content

The browsercookie module loads cookies used by your web browser into a cookiejar object. This can be useful if you want to use python to download the same content you see in the web browser without needing to login.

Install

pip install browsercookie

On Windows the builtin sqlite module will raise an error when loading the FireFox database. An updated version of sqlite can be installed with:

pip install pysqlite

Usage

Here is a hack to extract the title from a webpage:

>>> import re
>>> get_title = lambda html: re.findall('<title>(.*?)</title>', html, flags=re.DOTALL)[0].strip()

And here is the webpage title when downloaded normally:

>>> import urllib2
>>> url = 'https://bitbucket.org/'
>>> public_html = urllib2.urlopen(url).read()
>>> get_title(public_html)
'Git and Mercurial code management for teams'

Now let’s try with browsercookie - make sure you are logged into Bitbucket in Firefox before trying this example:

>>> import browsercookie
>>> cj = browsercookie.firefox()
>>> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
>>> login_html = opener.open(url).read()
>>> get_title(login_html)
'richardpenman / home &mdash; Bitbucket'

Differences with Python3:

>>> import urllib.request
>>> public_html = urllib.request.urlopen(url).read()
>>> opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

You should see your own username here, meaning the module successfully loaded the cookies from Firefox.

Here is an alternative example with requests, this time loading the Chrome cookies. Again make sure you are logged into Bitbucket in Chrome before running this:

>>> import requests
>>> cj = browsercookie.chrome()
>>> r = requests.get(url, cookies=cj)
>>> get_title(r.content)
'richardpenman / home &mdash; Bitbucket'

Alternatively if you don’t know/care which browser has the cookies you want then all available browser cookies can be loaded:

>>> cj = browsercookie.load()
>>> r = requests.get(url, cookies=cj)
>>> get_title(r.content)
'richardpenman / home &mdash; Bitbucket'

Contribute

So far the following platforms are supported:

  • Chrome: Linux, OSX, Windows

  • Firefox: Linux, OSX, Windows

However I only tested on a single version of each browser and so am not sure if the cookie sqlite format changes location or format in earlier/later versions. If you experience a problem please open an issue which includes details of the browser version and operating system. Also patches to support other browsers are very welcome, particularly for Internet Explorer on Windows.

Acknowledgements

Release files for browsercookie 0.7.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for browsercookie 0.7.9
File Size Uploaded
browsercookie-0.7.9.tar.gz 18.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for browsercookie 0.7.9
File Interpreter ABI Platform
browsercookie-0.7.9-py3-none-any.whl Python 3 none any Details

Total release size: 36.1 kB

Release files / browsercookie-0.7.9.tar.gz

Download URL browsercookie-0.7.9.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6537515409affd84040d66b7744d9f96d3dd4a89320349df7764c5d7d123d446
BLAKE2b-256 checksum
How to use checksums
77fb246517bd29d5706f0243b5113aa7e987b2caa1b705c7e7ebf8fc975d2c9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.6

Release files / browsercookie-0.7.9-py3-none-any.whl

Download URL browsercookie-0.7.9-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2bb0d9e644740a156f2f61964c329d3a0c87cf394103df5ab150f8ce2f93977e
BLAKE2b-256 checksum
How to use checksums
b1e7f9b621e3031d036fd8c8acdaf645ca031baffa7829f29b58d0c340111846
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.6

Release history Release notifications | RSS feed

0.8.2

2 release files

0.8.1

2 release files

This release

0.7.9 This release

2 release files

0.7.8

2 release files

0.7.7

1 release file

0.7.6

1 release file

0.7.5

1 release file

0.7.4

1 release file

0.7.3

1 release file

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page