微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Playwright Python异步打开多个页面?

如何解决如何使用Playwright Python异步打开多个页面?

我想使用Playwright for Python一次打开多个URL。但是我在努力寻找方法。这来自异步文档:

async def main():
    async with async_playwright() as p:
        for browser_type in [p.chromium,p.firefox,p.webkit]:
            browser = await browser_type.launch()
            page = await browser.newPage()
            await page.goto("https://scrapingant.com/")
            await page.screenshot(path=f"scrapingant-{browser_type.name}.png")
            await browser.close()

asyncio.get_event_loop().run_until_complete(main())

这将依次打开每个browser_type。如果要并行执行该怎么办?如果我想对网址列表执行类似的操作,该怎么办?

我尝试这样做:

urls = [
    "https://scrapethissite.com/pages/ajax-javascript/#2015","https://scrapethissite.com/pages/ajax-javascript/#2014",]
async def main(url):
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.newPage()
        await page.goto(url)
        await browser.close()

async def go_to_url():
    tasks = [main(url) for url in urls]
    await asyncio.wait(tasks)

go_to_url()

但这给了我以下错误

92: RuntimeWarning: coroutine 'go_to_url' was never awaited
  go_to_url()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

解决方法

我相信您需要使用相同的配方来调用go_to_url函数:

asyncio.get_event_loop().run_until_complete(go_to_url())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。