我使用python的requests创建了一个请求,并将请求响应的cookie保存到cookies.json中,并使用playwright将cookies添加到浏览器上下文中,但总是出错。playwright._impl._api_types。错误:cookies:预期数组,得到对象
# Save cookie
post = session.post(url, data=data, headers=headers, proxies=proxies)
req = post.cookies.get_dict()
with open('cookies.json', 'w+') as f:
save_cookies = json.dumps(req, ensure_ascii=False)
f.write(save_cookies)
# read cookie
async def worker(browser: Browser, i: int):
cookies = get_csrf_token_login(email_name, password)
context = await browser.new_context()
context.clear_cookies()
with open('cookies.json', 'r') as f:
cookie_test = json.load(f)
await context.add_cookies(cookie_test)
page = await context.new_page()
await page.goto(url)
print(await page.title())
# context
async def main():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=False, channel="chrome")
await asyncio.wait(
[asyncio.create_task(worker(browser, i)) for i in range(1)], # 创建上下文数量
return_when=asyncio.ALL_COMPLETED,
)
await browser.close()