SoFunction
Updated on 2025-04-27

Python implements the method of automatically clearing browser cookies at regular intervals

When I write about crawlers, I often click to browse too many pages, which leads to accumulating a lot of cookies.

Although a single cookie is small, long-term accumulation may take up the browser's storage space, causing the browser to run slower (especially for old devices).

Moreover, cookies (especially third-party cookies) may be used by advertisers or data analytics companies to track your browsing habits, build user portraits, and lead to precise advertising push.

But sometimes manual cleaning is a bit troublesome, I want to write a program to clean cookies regularly.

In Python, we can use the time module to set timing tasks and use browsercookie or browser-cookie3 library to clear browser cookies.

Here is a sample code showing how to automatically clear website cookies every once in a while.

Installation dependencies

First, you need to install the browser-cookie3 library, which can help you get and clear browser cookies.

pip install browser-cookie3

Sample code

import time
import browser_cookie3

def clear_cookies():
    # Get cookies for all browsers    cookies = browser_cookie3.load()
    
    # Clear all cookies    for cookie in cookies:
        ()
    
    print("Cookies cleared!")

def run_periodically(interval, function):
    while True:
        function()
        (interval)

# Clear cookies every 60 secondsrun_periodically(60, clear_cookies)

Code description

clear_cookies function: This function uses browser_cookie3.load() to get all cookies in the current browser, then iterates over these cookies and calls the delete() method to delete them.

run_periodically function: This function calls the clear_cookies function every specified interval (in seconds).

run_periodically(60, clear_cookies): This line of code sets the clear_cookies function to be called every 60 seconds.

Things to note

This code clears cookies for all browsers. If you only want to clear cookies for a specific website, you can add filtering criteria in the clear_cookies function.

The code needs to be run on a machine with a browser environment, because it depends on the browser's cookies storage.

If you are using a headless browser (such as Selenium), you may need to use Selenium's API to manage cookies.

Example of using Selenium to clear cookies

If you are using Selenium, you can use the following code to clear cookies:

from selenium import webdriver
import time

def clear_cookies(driver):
    driver.delete_all_cookies()
    print("Cookies cleared!")

def run_periodically(interval, function, driver):
    while True:
        function(driver)
        (interval)

# Start the browserdriver = ()

# Clear cookies every 60 secondsrun_periodically(60, clear_cookies, driver)

In addition to python writing, you can also manually clear:

Regular cleaning: manually delete cookies or set automatic browser clearance (such as Chrome's "Clear on Exit" feature).

Use Privacy Mode: Cookies are not saved by invisible browsing (such as Chrome's Incognito).

Restrict third-party cookies: Disable third-party cookies in browser settings to reduce tracking.

Selective retention: Keep the login status of commonly used websites (such as the whitelisting function) during cleaning.

Method supplement

Automatically clear specified cookies

To clear the specified cookies, we first need to get all cookies in the browser and then clear the specific cookies as needed. Here is a sample code that demonstrates how to clear a specified cookie using Python:

import requests

# Send HTTP request to obtain cookie informationresponse = ("
cookies = 

# Clear the specified cookieif 'cookie_name' in cookies:
    ('cookie_name')

# Send requests using updated cookie informationresponse = (" cookies=cookies)

2.selenium3+python automated acquisition and deletion of cookies

Complete code

from selenium import webdriver
from time import sleep
driver=()
#Start the browser to get cookiesprint(driver.get_cookies())
('http://127.0.0.1:8080/oa/')
#Get cookies after opening the home pageprint(driver.get_cookies())
#Get cookies after logging inloginInputs=driver.find_elements_by_class_name('loginInput')
loginInputs[0].send_keys('wangd')
loginInputs[1].send_keys('w1234567')
driver.find_element_by_id('button_submit').click()
sleep(2)
print(driver.get_cookies())

#Get specified cookiesprint(driver.get_cookie(name='Cookie_Lang_OA'))

# #Delete the specified cookie# driver.delete_cookie(name='LoginId_OA')
driver.delete_cookie(name='Cookie_Lang_OA')
# driver.delete_cookie(name='JSESSIONID')
print(driver.get_cookies())
#Refresh Verification()

# #Delete all cookies# driver.delete_all_cookies()
# print(driver.get_cookies())
# ()

sleep(2)
()

This is the article about Python's method to automatically clear browser cookies regularly. For more related content on Python's browser cookies, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!