GithubHelp home page GithubHelp logo

browsercookie's Introduction

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

bash

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:

bash

pip install pysqlite

Usage

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

python

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

python

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

python

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

python

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

python

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

python

>>> 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

browsercookie's People

Contributors

richardpenman avatar edanee avatar vadim-di avatar universum01 avatar bokrosbalint avatar timur-tabi avatar bxff avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.