File: README.md

package info (click to toggle)
browser-cookie3 0.20.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 188 kB
  • sloc: python: 1,595; makefile: 4
file content (164 lines) | stat: -rw-r--r-- 6,709 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
[![PyPi Downloads][PyPi-downloads]][PyPi-url]
[![PyPi Version][PyPi-version]][PyPi-url]
[![License][License-shield]][License-url]

This is a python3 fork of [Richard Penman's Browser Cookie](https://github.com/richardpenman/browsercookie)

# Browser Cookie

* ***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, Safari, W3m and Lynx.
* ***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.

## Install
```bash
pip install browser-cookie3
```

## Python usage

Here is a *dangerous* hack to extract the title from a webpage:
```python
#!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
#!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
#!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 &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](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:
```python
#!python

>>> import browser_cookie3
>>> import requests
>>> cj = browser_cookie3.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
#!python

>>> import browser_cookie3
>>> import requests
>>> cj = browser_cookie3.load()
>>> r = requests.get(url, cookies=cj)
>>> get_title(r.content)
'richardpenman / home &mdash; Bitbucket'
```

Alternatively if you are only interested in cookies from a specific domain, you can specify a domain filter.
```python
#!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 &mdash; Bitbucket'
```

## Command-line usage

Run `browser-cookie --help` for all options. Brief examples:

```sh
$ browser-cookie --firefox stackoverflow.com acct
t=BASE64_STRING_DESCRIBING_YOUR_STACKOVERFLOW_ACCOUNT

$ browser-cookie --json --chrome stackoverflow.com acct
{"version": 0, "name": "acct", "value": "t=BASE64_STRING_DESCRIBING_YOUR_STACKOVERFLOW_ACCOUNT",
"port_specified": false, "domain": ".stackoverflow.com", "domain_specified": true,
"domain_initial_dot": true, "path": "/", "path_specified": true, "secure": 1,
"expires": 1657049738, "discard": false, "rfc2109": false}

$ browser-cookie nonexistent-domain.com nonexistent-cookie && echo "Cookie found" || echo "No cookie found"
No cookie found
```

## Fresh cookie files
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!
### Chrome and chromium
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

## Contribute
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
* **W3m:** Linux
* **Lynx:** Linux

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

## Testing Dates  (dd/mm/yy)

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 |    -     |
W3m      | 05/07/23 |    -     |    -     |
Lynx     | 05/07/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](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.

## Acknowledgements ##
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/).

[PyPi-downloads]: https://img.shields.io/pypi/dm/browser-cookie3
[PyPi-url]: https://pypi.org/project/browser-cookie3/
[License-shield]: https://img.shields.io/github/license/borisbabic/browser_cookie3?color=00aaaa
[License-url]: https://github.com/borisbabic/browser_cookie3/blob/master/LICENSE
[PyPi-version]: https://img.shields.io/pypi/v/browser-cookie3?color=00aa00