Download Cookies from Chrome and Firefox with Python
What Cookies are?
A cookie is a string stored in our browser (client-side) really useful for many (really many) reasons.
For example, since the http connection are stateless (the server doesn’t store any information about us), with the cookies we can still have active sessions.
Without cookies you will not able to find your website cart (e.g. Zara.com) full after a first visit.
At the other hands cookies are really helpful for the server to check same critical parameters (e.g. check if your session is fresh).
Coming back to the point
There are many reasons why you may need to download cookies from your browser.
In most cases, but not only, you want to send requests via Python and you need to populate them with cookies or with all those useful values (session, request id, csrf etc.) for the request to be successful.
Whatever your reasons (even just seeing how many and which cookies your browser has) in this small guide I will explain how to download them through very few lines of code with python.
ALERT: Where talking about requests with Python so I’m supposing that you are good with python and you already installed it!
1. Install Chrome (or Firefox)
For this guide we will use Chrome (also Firefox is possible). I’m sure that it will be a way to do the same in other browser. But why reinvent the wheel if you just need to download an app?
2. Install browser_cookie3
To download cookies from your chrome browser you’ll need only one package: browser-cookie3.
This package is a python3 fork of [Richard Penman’s Browser Cookie]
From the official page of the package (read always the documentation) we can see whats it does:
- 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? Currently Chrome and Firefox.
- How are the cookies stored? In a sqlite database in your home directory.
So let’s open your terminal and text it: pip3 install browser-cookie3
Press Enter and go to the next step after the installation.
3. Code!
In few lines of code you can download all the cookies you need (from a specified domain) as the following:
To be precise, the previous function do the following stuffs:
- Import all the cookies (from chrome) and store them in a list
- For each cookie check if the domain is the one specified (e.g. facebook.com)
- If a cookieName is specified as argument return the cookie in the domain with this name as a dictionary structure
- If cookie is not found or there are errors within the names (domain or cookieName) returns {}
Note that: if you don’t specify a cookieName the function will return all the cookies from the specified domain.
- Arts
- Business
- Computers
- Games
- Health
- Home
- Kids and Teens
- Money
- News
- Recreation
- Reference
- Regional
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World