|
| 1 | +"""Session Resume: reconnect to an existing Cloud Browser session""" |
| 2 | +import time |
| 3 | +from scrapfly import ScrapflyClient, BrowserConfig |
| 4 | +from playwright.sync_api import sync_playwright |
| 5 | + |
| 6 | +scrapfly = ScrapflyClient(key='__API_KEY__') |
| 7 | + |
| 8 | +SESSION_ID = 'my-persistent-session' |
| 9 | + |
| 10 | +# Configure with session + auto_close=False for persistence |
| 11 | +browser_config = BrowserConfig( |
| 12 | + proxy_pool='datacenter', |
| 13 | + session=SESSION_ID, |
| 14 | + auto_close=False, |
| 15 | +) |
| 16 | + |
| 17 | +cdp_url = scrapfly.cloud_browser(browser_config) |
| 18 | + |
| 19 | + |
| 20 | +def first_connection(): |
| 21 | + """First connection: navigate and set cookies""" |
| 22 | + print('=== First Connection ===') |
| 23 | + with sync_playwright() as p: |
| 24 | + browser = p.chromium.connect_over_cdp(cdp_url) |
| 25 | + context = browser.contexts[0] |
| 26 | + page = context.new_page() |
| 27 | + page.goto('https://web-scraping.dev') |
| 28 | + |
| 29 | + # Set a cookie |
| 30 | + context.add_cookies([{ |
| 31 | + 'name': 'session_token', |
| 32 | + 'value': 'abc123', |
| 33 | + 'domain': 'web-scraping.dev', |
| 34 | + 'path': '/' |
| 35 | + }]) |
| 36 | + |
| 37 | + print('Cookies set, disconnecting...') |
| 38 | + browser.close() # Disconnects CDP - browser stays alive (auto_close=false) |
| 39 | + |
| 40 | + |
| 41 | +def second_connection(): |
| 42 | + """Second connection: cookies are still there""" |
| 43 | + print('=== Second Connection (Resume) ===') |
| 44 | + with sync_playwright() as p: |
| 45 | + browser = p.chromium.connect_over_cdp(cdp_url) |
| 46 | + context = browser.contexts[0] |
| 47 | + page = context.pages[0] if context.pages else context.new_page() |
| 48 | + |
| 49 | + # Cookies are still there! |
| 50 | + cookies = context.cookies('https://web-scraping.dev') |
| 51 | + print('Cookies from previous session:', cookies) |
| 52 | + |
| 53 | + browser.close() # Disconnects CDP |
| 54 | + |
| 55 | + |
| 56 | +first_connection() |
| 57 | +time.sleep(2) # Wait a bit, then reconnect |
| 58 | +second_connection() |
| 59 | + |
| 60 | +# Terminate the session when fully done |
| 61 | +scrapfly.cloud_browser_session_stop(SESSION_ID) |
| 62 | +print(f'Session {SESSION_ID} terminated') |
0 commit comments