Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from playwright.sync_api import sync_playwright
- def get_cookie(url: str, btn: str) -> list:
- """
- This function obtains an authentication token from the cookies of a given URL.
- The function specifically uses the Chromium web browser through the playwright library
- to open the specified URL, accepts the site's cookies, and then extracts any potential
- authentication tokens present in these cookies.
- Args:
- url (str): The URL from which the authentication token will be extracted.
- btn (str): CSS selector of `Accept Cookies` button on page
- Returns:
- str: The extracted authentication token. If no auth token found, it returns empty string.
- """
- with sync_playwright() as p:
- browser = p.chromium.launch()
- context = browser.new_context()
- page = context.new_page()
- page.goto(url)
- page.click(btn)
- return context.cookies()
- cookie = get_cookie("https://www.travisperkins.co.uk/", "#onetrust-accept-btn-handler")
- print(cookie)
Advertisement
Add Comment
Please, Sign In to add comment