【问题标题】:Python async Playwright pass data outside functionPython async Playwright 在函数外传递数据
【发布时间】:2022-10-18 21:00:08
【问题描述】:

我对异步编程很陌生,我只是无法从函数中获取 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 数据并将其进一步用于其他东西。

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

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

谢谢您的帮助!

【问题讨论】:

    标签: python async-await playwright


    【解决方案1】:

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

    我使用他的建议两次使用未来来获取 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
    

    【讨论】:

      【解决方案2】:

      你可以使用 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()
      

      【讨论】:

      • 嗨乔兰,德克萨斯州。尝试了这个解决方案,它似乎是我需要的东西。但是执行脚本后似乎永远等待并且永远不会收到数据。 'Made call' 被打印出来,但其余的没有。
      • 在导航步骤之后放置result_json = await myRespPromise print("GOT RESULT:",result_json) 后,我得到了它的工作。所以更接近了:) 现在只需要在 main() 函数之外获取结果。
      • 返回那个承诺并在其他地方等待它......
      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多