Skip to main content

Loads cookies from your browser into a cookiejar object so can download with urllib and other libraries the same content you see in the web browser.

Project description

Browser Cookie

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 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
  • Brave: 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

browsercookie-0.8.2.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

browsercookie-0.8.2-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file browsercookie-0.8.2.tar.gz.

File metadata

  • Download URL: browsercookie-0.8.2.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for browsercookie-0.8.2.tar.gz
Algorithm Hash digest
SHA256 383e7e6d5a5c3bc6c837d6d1e1c81b1d082695b1c72bb949976f41220da23a90
MD5 c8049562990960848222817cca35d342
BLAKE2b-256 23eb32558ce08469fdac7f65d4e1a268b441d195a4da63dab7fc9d026d677468

See more details on using hashes here.

File details

Details for the file browsercookie-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: browsercookie-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for browsercookie-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d2773fc96a413c5276e6f4f184fd43dab7aba823763546e8a7542e76b40ce1a0
MD5 b06a313f31e79cab5d767e33c4d667a7
BLAKE2b-256 ce7d028b81866b67822e12320e1cdcddc68178d35a27ed159326f5d22344f086

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page