0
from playwright.sync_api import sync_playwright

url = "https://hls-js.netlify.app/demo/" 
with sync_playwright() as p: 

    browser = p.chromium.launch(channel = "chrome")
    page = browser.new_page() 
    page.on("response", lambda response: print(response.url) if (".m3u8" in response.url) else False) 
    page.goto(url) 
    page.wait_for_timeout(10*1000) 
    browser.close()

Hi guys, i need to pass the "response.url" inside a variable that I can use further in the code. I don't want response.url printed out, I just want to save that string in a variable. How can I do this?

1 Answers1

1

Functions assigned to events (in any module) can't return value and you may need to use global variable for this. But lambda can't use global and can't assign value to variable so it needs normal function

url = ""

def on_response(response):
    global url  # inform functio to use external variable (instead of local variable) when you use `=` to assign value
    
    if ".m3u8" in response.url:
        url = response.url

page.on("response", on_response) 
furas
  • 134,197
  • 12
  • 106
  • 148