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

The browser_cookie 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 browser-cookie

Note only Python 2 is supported currently.

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 browser_cookie - make sure you are logged into Bitbucket in Firefox before trying this example:

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

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 = browser_cookie.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 = browser_cookie.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

  • 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 Chrome and Internet Explorer on Windows.

Acknowledgements

Special thanks to Nathan Henrie for his example of how to decode the Chrome cookies.

Project details


Supported by

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