(.*?)

decibyte/browser_cookie3

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

#!python >>> import re >>> get_title = lambda html: re.findall('', 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 browser_cookie3 — make sure you are logged into Bitbucket in Firefox before trying this example:

#!python >>> import browser_cookie3 >>> cj = browser_cookie3.firefox() >>> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) >>> login_html = opener.open(url).read() >>> get_title(login_html) 'richardpenman / home — 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:

#!python >>> import browser_cookie3 >>> import requests >>> cj = browser_cookie3.chrome() >>> r = requests.get(url, cookies=cj) >>> get_title(r.content) 'richardpenman / home — Bitbucket'

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

#!python >>> import browser_cookie3 >>> import requests >>> cj = browser_cookie3.load() >>> r = requests.get(url, cookies=cj) >>> get_title(r.content) 'richardpenman / home — Bitbucket'

Alternatively if you are only interested in cookies from a specific domain, you can specify a domain filter.

#!python >>> import browser_cookie3 >>> import requests >>> cj = browser_cookie3.chrome(domain_name='www.bitbucket.com') >>> r = requests.get(url, cookies=cj) >>> get_title(r.content) 'richardpenman / home — Bitbucket'

Creating and testing a fresh cookie file can help eliminate some possible user specific issues. It also allows you to upload a cookie file you are having issus with, since you should never upload your main cookie file!

For linux and assumably mac:

Run google-chrome-stable —user-data-dir=browser_cookie3 #replace google-chrome-stable with your command to start chrome/chromium and when you close the browser you will have a new cookie file at browser_cookie3/Default/Cookies

Читайте также:  Python setup py example

If you want to share a cookie file then visit some site that will generate cookie (without logging in!), example https://www.theverge.com/ will save cookies after you accept the GDPR notice.

Planned backwards incompatible changes for 1.0

  • more sensible cookie file checking order, like first using the default defined in profiles.ini for firefox

So far the following platforms are supported:

  • Chrome: Linux, OSX, Windows
  • Firefox: Linux, OSX, Windows
  • Opera: Linux, OSX, Windows
  • Opera GX: OSX, Windows
  • Edge: Linux, OSX, Windows
  • Chromium: Linux, OSX, Windows
  • Brave: Linux, OSX, Windows
  • Vivaldi: Linux, OSX, Windows
  • Safari: OSX
OS Chrome Firefox Opera Opera GX Edge Chromium Brave Vivaldi Safari
Mac 31/01/23 09/12/20 31/01/23 31/01/23 31/01/23 15/06/22 31/01/23 31/01/23 31/01/23
Linux 31/01/23 09/12/20 31/01/23 31/01/23 07/24/21 31/01/23 31/01/23
Windows 31/01/23 09/12/20 31/01/23 31/01/23 31/01/23 15/06/22 31/01/23 15/06/22

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.

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

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

This is a fork of browser_cookie

License

borisbabic/browser_cookie3

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Читайте также:  Java code for panel

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

  • What does it do? Loads cookies used by your web browser into a cookiejar object.
  • Why is it useful? This means you can use python to download and get the same content you see in the web browser without needing to login.
  • Which browsers are supported? Chrome, Firefox, LibreWolf, Opera, Opera GX, Edge, Chromium, Brave, Vivaldi, and Safari.
  • How are the cookies stored? All currently-supported browsers store cookies in a sqlite database in your home directory.
pip install browser-cookie3

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

#!python >>> import re >>> get_title = lambda html: re.findall('', 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 browser_cookie3 — make sure you are logged into Bitbucket in Firefox before trying this example:

#!python >>> import browser_cookie3 >>> cj = browser_cookie3.firefox() >>> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) >>> login_html = opener.open(url).read() >>> get_title(login_html) 'richardpenman / home — 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:

#!python >>> import browser_cookie3 >>> import requests >>> cj = browser_cookie3.chrome() >>> r = requests.get(url, cookies=cj) >>> get_title(r.content) 'richardpenman / home — Bitbucket'

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

#!python >>> import browser_cookie3 >>> import requests >>> cj = browser_cookie3.load() >>> r = requests.get(url, cookies=cj) >>> get_title(r.content) 'richardpenman / home — Bitbucket'

Alternatively if you are only interested in cookies from a specific domain, you can specify a domain filter.

#!python >>> import browser_cookie3 >>> import requests >>> cj = browser_cookie3.chrome(domain_name='www.bitbucket.com') >>> r = requests.get(url, cookies=cj) >>> get_title(r.content) 'richardpenman / home — Bitbucket'

Creating and testing a fresh cookie file can help eliminate some possible user specific issues. It also allows you to upload a cookie file you are having issues with, since you should never upload your main cookie file!

Читайте также:  Python freeze to file

For linux and assumably mac:

Run google-chrome-stable —user-data-dir=browser_cookie3 #replace google-chrome-stable with your command to start chrome/chromium and when you close the browser you will have a new cookie file at browser_cookie3/Default/Cookies

If you want to share a cookie file then visit some site that will generate cookie (without logging in!), example https://www.theverge.com/ will save cookies after you accept the GDPR notice.

Planned backwards incompatible changes for 1.0

  • more sensible cookie file checking order, like first using the default defined in profiles.ini for FireFox-based browsers

So far the following platforms are supported:

  • Chrome: Linux, MacOS, Windows
  • Firefox: Linux, MacOS, Windows
  • LibreWolf: Linux, MacOS, Windows
  • Opera: Linux, MacOS, Windows
  • Opera GX: MacOS, Windows
  • Edge: Linux, MacOS, Windows
  • Chromium: Linux, MacOS, Windows
  • Brave: Linux, MacOS, Windows
  • Vivaldi: Linux, MacOS, Windows
  • Safari: MacOS

You are welcome to contribute support for other browsers, or other platforms.

Browser Linux MacOS Windows
Chrome 31/01/23 31/01/23 31/01/23
Firefox 05/06/23 05/06/23 05/06/23
LibreWolf 05/06/23 05/06/23 05/06/23
Opera 31/01/23 31/01/23 31/01/23
Opera GX 31/01/23 31/01/23
Edge 31/01/23 31/01/23 31/01/23
Chromium 07/24/21 15/06/22 15/06/22
Brave 31/01/23 31/01/23 31/01/23
Vivaldi 31/01/23 31/01/23 15/06/22
Safari 31/01/23

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.

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

Источник

Оцените статью