| 1 |
[![PyPi Downloads][PyPi-downloads]][PyPi-url] |
| 2 |
[![PyPi Version][PyPi-version]][PyPi-url] |
| 3 |
[![License][License-shield]][License-url] |
| 4 |
|
| 5 |
This is a python3 fork of [Richard Penman's Browser Cookie](https://github.com/richardpenman/browsercookie) |
| 6 |
|
| 7 |
# Browser Cookie |
| 8 |
|
| 9 |
* ***What does it do?*** Loads cookies used by your web browser into a cookiejar object. |
| 10 |
* ***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. |
| 11 |
* ***Which browsers are supported?*** Chrome, Firefox, LibreWolf, Opera, Opera GX, Edge, Chromium, Brave, Vivaldi, Safari, W3m and Lynx. |
| 12 |
* ***How are the cookies stored?*** Most currently-supported browsers store cookies in a sqlite database in your home directory. Some browsers store them in tab-separated txt files. |
| 13 |
|
| 14 |
## Install |
| 15 |
```bash |
| 16 |
pip install browser-cookie3 |
| 17 |
``` |
| 18 |
|
| 19 |
## Python usage |
| 20 |
|
| 21 |
Here is a *dangerous* hack to extract the title from a webpage: |
| 22 |
```python |
| 23 |
#!python |
| 24 |
|
| 25 |
>>> import re |
| 26 |
>>> get_title = lambda html: re.findall('<title>(.*?)</title>', html, flags=re.DOTALL)[0].strip() |
| 27 |
``` |
| 28 |
|
| 29 |
And here is the webpage title when downloaded normally: |
| 30 |
```python |
| 31 |
#!python |
| 32 |
|
| 33 |
>>> import urllib2 |
| 34 |
>>> url = 'https://bitbucket.org/' |
| 35 |
>>> public_html = urllib2.urlopen(url).read() |
| 36 |
>>> get_title(public_html) |
| 37 |
'Git and Mercurial code management for teams' |
| 38 |
``` |
| 39 |
|
| 40 |
Now let's try with browser_cookie3 - make sure you are logged into Bitbucket in Firefox before trying this example: |
| 41 |
```python |
| 42 |
#!python |
| 43 |
|
| 44 |
>>> import browser_cookie3 |
| 45 |
>>> cj = browser_cookie3.firefox() |
| 46 |
>>> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) |
| 47 |
>>> login_html = opener.open(url).read() |
| 48 |
>>> get_title(login_html) |
| 49 |
'richardpenman / home — Bitbucket' |
| 50 |
``` |
| 51 |
|
| 52 |
You should see your own username here, meaning the module successfully loaded the cookies from Firefox. |
| 53 |
|
| 54 |
Here is an alternative example with [requests](http://docs.python-requests.org/en/latest/), this time loading the Chrome cookies. Again make sure you are logged into Bitbucket in Chrome before running this: |
| 55 |
```python |
| 56 |
#!python |
| 57 |
|
| 58 |
>>> import browser_cookie3 |
| 59 |
>>> import requests |
| 60 |
>>> cj = browser_cookie3.chrome() |
| 61 |
>>> r = requests.get(url, cookies=cj) |
| 62 |
>>> get_title(r.content) |
| 63 |
'richardpenman / home — Bitbucket' |
| 64 |
``` |
| 65 |
|
| 66 |
Alternatively if you don't know/care which browser has the cookies you want then all available browser cookies can be loaded: |
| 67 |
```python |
| 68 |
#!python |
| 69 |
|
| 70 |
>>> import browser_cookie3 |
| 71 |
>>> import requests |
| 72 |
>>> cj = browser_cookie3.load() |
| 73 |
>>> r = requests.get(url, cookies=cj) |
| 74 |
>>> get_title(r.content) |
| 75 |
'richardpenman / home — Bitbucket' |
| 76 |
``` |
| 77 |
|
| 78 |
Alternatively if you are only interested in cookies from a specific domain, you can specify a domain filter. |
| 79 |
```python |
| 80 |
#!python |
| 81 |
|
| 82 |
>>> import browser_cookie3 |
| 83 |
>>> import requests |
| 84 |
>>> cj = browser_cookie3.chrome(domain_name='www.bitbucket.com') |
| 85 |
>>> r = requests.get(url, cookies=cj) |
| 86 |
>>> get_title(r.content) |
| 87 |
'richardpenman / home — Bitbucket' |
| 88 |
``` |
| 89 |
|
| 90 |
## Command-line usage |
| 91 |
|
| 92 |
Run `browser-cookie --help` for all options. Brief examples: |
| 93 |
|
| 94 |
```sh |
| 95 |
$ browser-cookie --firefox stackoverflow.com acct |
| 96 |
t=BASE64_STRING_DESCRIBING_YOUR_STACKOVERFLOW_ACCOUNT |
| 97 |
|
| 98 |
$ browser-cookie --json --chrome stackoverflow.com acct |
| 99 |
{"version": 0, "name": "acct", "value": "t=BASE64_STRING_DESCRIBING_YOUR_STACKOVERFLOW_ACCOUNT", |
| 100 |
"port_specified": false, "domain": ".stackoverflow.com", "domain_specified": true, |
| 101 |
"domain_initial_dot": true, "path": "/", "path_specified": true, "secure": 1, |
| 102 |
"expires": 1657049738, "discard": false, "rfc2109": false} |
| 103 |
|
| 104 |
$ browser-cookie nonexistent-domain.com nonexistent-cookie && echo "Cookie found" || echo "No cookie found" |
| 105 |
No cookie found |
| 106 |
``` |
| 107 |
|
| 108 |
## Fresh cookie files |
| 109 |
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! |
| 110 |
### Chrome and chromium |
| 111 |
For linux and assumably mac: |
| 112 |
|
| 113 |
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` |
| 114 |
|
| 115 |
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. |
| 116 |
|
| 117 |
## Planned backwards incompatible changes for 1.0 |
| 118 |
- more sensible cookie file checking order, like first using the default defined in profiles.ini for FireFox-based browsers |
| 119 |
|
| 120 |
## Contribute |
| 121 |
So far the following platforms are supported: |
| 122 |
|
| 123 |
* **Chrome:** Linux, MacOS, Windows |
| 124 |
* **Firefox:** Linux, MacOS, Windows |
| 125 |
* **LibreWolf:** Linux, MacOS, Windows |
| 126 |
* **Opera:** Linux, MacOS, Windows |
| 127 |
* **Opera GX:** MacOS, Windows |
| 128 |
* **Edge:** Linux, MacOS, Windows |
| 129 |
* **Chromium:** Linux, MacOS, Windows |
| 130 |
* **Brave:** Linux, MacOS, Windows |
| 131 |
* **Vivaldi:** Linux, MacOS, Windows |
| 132 |
* **Safari:** MacOS |
| 133 |
* **W3m:** Linux |
| 134 |
* **Lynx:** Linux |
| 135 |
|
| 136 |
You are welcome to contribute support for other browsers, or other platforms. |
| 137 |
|
| 138 |
## Testing Dates (dd/mm/yy) |
| 139 |
|
| 140 |
Browser | Linux | MacOS | Windows | |
| 141 |
:------ | :------: | :------: | :------: | |
| 142 |
Chrome | 31/01/23 | 31/01/23 | 31/01/23 | |
| 143 |
Firefox | 05/06/23 | 05/06/23 | 05/06/23 | |
| 144 |
LibreWolf| 05/06/23 | 05/06/23 | 05/06/23 | |
| 145 |
Opera | 31/01/23 | 31/01/23 | 31/01/23 | |
| 146 |
Opera GX | - | 31/01/23 | 31/01/23 | |
| 147 |
Edge | 31/01/23 | 31/01/23 | 31/01/23 | |
| 148 |
Chromium | 07/24/21 | 15/06/22 | 15/06/22 | |
| 149 |
Brave | 31/01/23 | 31/01/23 | 31/01/23 | |
| 150 |
Vivaldi | 31/01/23 | 31/01/23 | 15/06/22 | |
| 151 |
Safari | - | 31/01/23 | - | |
| 152 |
W3m | 05/07/23 | - | - | |
| 153 |
Lynx | 05/07/23 | - | - | |
| 154 |
|
| 155 |
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](https://github.com/borisbabic/browser_cookie3/issues/new) 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. |
| 156 |
|
| 157 |
## Acknowledgements ## |
| 158 |
Special thanks to Nathan Henrie for his example of [how to decode the Chrome cookies](http://n8henrie.com/2013/11/use-chromes-cookies-for-easier-downloading-with-python-requests/). |
| 159 |
|
| 160 |
[PyPi-downloads]: https://img.shields.io/pypi/dm/browser-cookie3 |
| 161 |
[PyPi-url]: https://pypi.org/project/browser-cookie3/ |
| 162 |
[License-shield]: https://img.shields.io/github/license/borisbabic/browser_cookie3?color=00aaaa |
| 163 |
[License-url]: https://github.com/borisbabic/browser_cookie3/blob/master/LICENSE |
| 164 |
[PyPi-version]: https://img.shields.io/pypi/v/browser-cookie3?color=00aa00 |