-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.py
39 lines (32 loc) · 1.42 KB
/
cookies.py
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
import pickle
import pprint
import time
from selenium import webdriver
driver = webdriver.Chrome('/Users/favourkelvin/Downloads/chromedriver')
def save_cookies(driver, location):
pickle.dump(driver.get_cookies(), open(location, "wb"))
def load_cookies(driver, location, url=None):
cookies = pickle.load(open(location, "rb"))
driver.delete_all_cookies()
# have to be on a page before you can add any cookies, any page - does not matter which
driver.get("https://google.com" if url is None else url)
for cookie in cookies:
if isinstance(cookie.get('expiry'), float):#Checks if the instance expiry a float
cookie['expiry'] = int(cookie['expiry'])# it converts expiry cookie to a int
driver.add_cookie(cookie)
def delete_cookies(driver, domains=None):
if domains is not None:
cookies = driver.get_cookies()
original_len = len(cookies)
for cookie in cookies:
if str(cookie["domain"]) in domains:
cookies.remove(cookie)
if len(cookies) < original_len: # if cookies changed, we will update them
# deleting everything and adding the modified cookie object
driver.delete_all_cookies()
for cookie in cookies:
driver.add_cookie(cookie)
else:
driver.delete_all_cookies()
#path to save the cookies
cookies_location = "/Users/favourkelvin/Documents/recipe-links-crawler/cookies.txt"