Python async Playwright 在函数外传递数据

Python async Playwright pass data outside function

我是异步编程的新手,我无法从函数中获取 json 数据。是否有某种特殊的方式从异步函数传递数据?我想使用 json 数据来提取其他数据。

async def main():
    async with async_playwright() as p:

        async def handle_response(response): 
        # the endpoint we are insterested in 
            if ('eindpoint/name' in response.url): 
                json_data = await response.json()

                print((json_data))

        browser = await p.chromium.launch()
        page = await browser.new_page()
                
        # go to directly to searchpage
        await page.goto("website_url", wait_until='networkidle')
        
        page.on('response', handle_response)

        await page.fill('input[id=zoeklocatie]', 'search_query')
        
        # Use two enters to first make button visible
        await page.keyboard.press("Enter")
        await page.keyboard.press("Enter")
        
        await page.wait_for_timeout(3000)
        
        await browser.close()
    
await main()

现在的结果是打印了JSON数据。但是我怎样才能在函数之外获取这个 JSON 数据并将其进一步用于其他东西。

我尝试 return 数据和变量。使用全局变量。但是 return 值一直为空,我认为这与代码的异步工作有关。所以 return 比结果早。

任何人都知道我是否正确以及我该如何解决这个问题?

感谢您的帮助!

你可以使用 Future(就像 JS 中的 Promise)

async def main():
    async with async_playwright() as p:
        myRespPromise = asyncio.Future()
        async def handle_response(response): 
        # the endpoint we are insterested in 
            if ('eindpoint/name' in response.url): 
                json_data = await response.json()
                # "resolve the promise"
                myRespPromise.set_result(json_data)

        browser = await p.chromium.launch()
        page = await browser.new_page()
                
        # go to directly to searchpage
        await page.goto("website_url", wait_until='networkidle')
        
        page.on('response', handle_response)
        print("Made call, now await response...")
        result_json = await myRespPromise
        print("GOT RESULT:",result_json)

        await page.fill('input[id=zoeklocatie]', 'search_query')
        
        # Use two enters to first make button visible
        await page.keyboard.press("Enter")
        await page.keyboard.press("Enter")
        
        await page.wait_for_timeout(3000)
        
        await browser.close()
    
await main()

所以在 Joran 的帮助下,我能够使这段代码工作,tx!

我按照他的建议使用 future 两次来获取 main() 函数之外的数据。

mainRespPromise = asyncio.Future()
    async def main():
        async with async_playwright() as p:
            myRespPromise = asyncio.Future()
            async def handle_response(response): 
            # the endpoint we are insterested in 
                if ('eindpoint/name' in response.url): 
                    json_data = await response.json()
                    # "resolve the promise"
                    myRespPromise.set_result(json_data)
    
            browser = await p.chromium.launch()
            page = await browser.new_page()
                    
            # go to directly to searchpage
            await page.goto("website_url", wait_until='networkidle')
            
            page.on('response', handle_response)
            print("Made call, now await response...")

            await page.fill('input[id=zoeklocatie]', 'search_query')
            
            # Use two enters to first make button visible
            await page.keyboard.press("Enter")
            await page.keyboard.press("Enter")
            
            result_json = await myRespPromise
            print("GOT RESULT:",result_json)

            await page.wait_for_timeout(3000)
            
            await browser.close()
            mainRespPromise.set_result(result_json)
    
    await main()

    json_data = await mainRespPromise