DECROMAX

Get session cookie

Mar 13th, 2023 (edited)
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | Software | 0 0
  1. from playwright.sync_api import sync_playwright
  2.  
  3. def get_cookie(url: str, btn: str) -> list:
  4.        """
  5.       This function obtains an authentication token from the cookies of a given URL.
  6.  
  7.       The function specifically uses the Chromium web browser through the playwright library
  8.       to open the specified URL, accepts the site's cookies, and then extracts any potential
  9.       authentication tokens present in these cookies.
  10.  
  11.       Args:
  12.           url (str): The URL from which the authentication token will be extracted.
  13.           btn (str): CSS selector of `Accept Cookies` button on page
  14.  
  15.       Returns:
  16.           str: The extracted authentication token. If no auth token found, it returns empty string.
  17.       """
  18.     with sync_playwright() as p:
  19.         browser = p.chromium.launch()
  20.         context = browser.new_context()
  21.         page = context.new_page()
  22.         page.goto(url)
  23.         page.click(btn)
  24.         return context.cookies()
  25.  
  26. cookie = get_cookie("https://www.travisperkins.co.uk/", "#onetrust-accept-btn-handler")
  27. print(cookie)
Tags: playright
Advertisement
Add Comment
Please, Sign In to add comment