From a31aa1e6d84031a937072ae46c548ea76e4aa580 Mon Sep 17 00:00:00 2001 From: Thephaseless Date: Thu, 12 Dec 2024 23:45:24 +0000 Subject: [PATCH] save cookies between requests --- main.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/main.py b/main.py index 11f6bab..bf2bd8f 100644 --- a/main.py +++ b/main.py @@ -19,6 +19,8 @@ app = FastAPI(debug=LOG_LEVEL == logging.DEBUG, log_level=LOG_LEVEL) +cookies = [] + @app.get("/") def read_root(): @@ -56,6 +58,10 @@ def read_item(request: LinkRequest) -> LinkResponse: with SB(uc=True, locale_code="en", test=False, xvfb=True, ad_block=True) as sb: try: sb: BaseCase + global cookies # noqa: PLW0603 + if cookies: + sb.uc_open_with_reconnect(request.url) + sb.add_cookies(cookies) sb.uc_open_with_reconnect(request.url) source = sb.get_page_source() source_bs = BeautifulSoup(source, "html.parser") @@ -86,6 +92,7 @@ def read_item(request: LinkRequest) -> LinkResponse: ), startTimestamp=start_time, ) + cookies = sb.get_cookies() except Exception as e: logger.error(f"Error: {e}") if sb.driver: