for key, value in other: ValueError: too many values to unpack (expected 2)(pickle)

for key, value in other: ValueError: too many values to unpack (expected 2)(pickle)

我使用此代码获取 cookie 并将它们保存到文件

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import os

import requests, pickle


user = os.getlogin()
options = webdriver.ChromeOptions()
# options.headless = True
options.add_argument('--profile-directory=Default')
options.add_argument(
    f'--user-data-dir=C:\Users\{user}\AppData\Local\Google\Chrome\User Data')

PATH = os.path.dirname(__file__) + "\chromedriver.exe"
driver = webdriver.Chrome(
    executable_path=PATH, chrome_options=options)

driver.get("https://web.whatsapp.com")
WebDriverWait(driver, 5000).until(
    EC.presence_of_element_located((By.XPATH, '//*[@id="side"]/header/div[2]/div/span/div[2]/div')))
cook = driver.get_cookies()

pickle.dump( cook , open("cookies.pkl","wb"))

但是当我想阅读它们时给我一个错误

代码:

session = requests.session()  

with open('cookies.pkl', 'rb') as f:
    session.cookies.update(pickle.load(f))

错误:

for key, value in other:
ValueError: too many values to unpack (expected 2)

Thanks again if anyone can recommend a better way to read and write cookies in the file

您的 cookies.pkl 文件包含一个列表,而 session.cookies.update 需要一个字典,因此要么遍历您的列表并多次更新,要么从列表中选择正确的 cookie 并使用它进行更新