You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Google Colab运行Playwright遇事件循环错误,求可行解决方案

在Google Colab中配置并使用Playwright的可行方案

问题背景

在Google Colab中运行Playwright时,分别尝试同步API和异步API均遭遇事件循环相关错误:

1. 同步API报错情况

使用以下同步代码:

!pip install playwright
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.firefox.launch(headless=True)
    page = browser.new_page()
    page.goto("https://www.google.com")

    page.wait_for_timeout(3000)
    browser.close()

触发报错:

ERROR:root:An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line string', (1, 33))

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-29-bc0f59648c4a> in <module>()
      1 from playwright.sync_api import sync_playwright
      2 
----> 3 with sync_playwright() as p:
      4     browser = p.firefox.launch(headless=True)
      5     page = browser.new_page()

/usr/local/lib/python3.7/dist-packages/playwright/sync_api/_context_manager.py in __enter__(self)
     44             raise Error(
     45                 """It looks like you are using Playwright Sync API inside the asyncio loop.
---> 46 Please use the Async API instead."""
     47             )
     48 

Error: It looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead.

2. 异步API报错情况

改用异步代码后:

import time
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page(storage_state='auth.json')
        await page.goto('https://www.instagram.com/explore/tags/alanzoka/')
        time.sleep(6)
        html = await page.content()

        time.sleep(5)

        # await browser.close()


asyncio.run(main())

仍触发报错:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-34-c582898e6ee9> in <module>()
     27 
     28 
---> 29 asyncio.run(main())

/usr/lib/python3.7/asyncio/runners.py in run(main, debug)
     32     if events._get_running_loop() is not None:
     33         raise RuntimeError(
---> 34             "asyncio.run() cannot be called from a running event loop")
     35 
     36     if not coroutines.iscoroutine(main):

RuntimeError: asyncio.run() cannot be called from a running event loop

解决方案

步骤1:安装Playwright及浏览器依赖

首先在Colab单元格中执行安装命令:

!pip install playwright
!playwright install

步骤2:正确使用异步API

Colab运行环境默认处于一个活跃的asyncio事件循环中,因此不能用asyncio.run()启动新循环,直接await异步函数即可。同时需避免在异步代码中使用阻塞式的time.sleep(),改用Playwright内置的异步等待方法:

import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        # Colab建议使用无头模式运行浏览器
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        # 访问目标页面
        await page.goto("https://www.google.com")
        # 使用Playwright异步等待替代time.sleep
        await page.wait_for_timeout(3000)
        # 获取页面内容
        html_content = await page.content()
        # 打印部分内容验证
        print(html_content[:500])
        # 关闭浏览器
        await browser.close()

# 在Colab中直接await执行异步函数
await main()

关键注意事项

  • 避免同步API:Colab环境默认运行在asyncio事件循环内,Playwright同步API不支持这种场景,必须使用异步API。
  • 禁用time.sleep:异步代码中使用time.sleep()会阻塞整个事件循环,改用await page.wait_for_timeout(毫秒数)或await asyncio.sleep(秒数)。
  • 无头模式:Colab无图形界面,建议始终启用headless=True运行浏览器,避免显示相关异常。
  • 认证状态处理:如果需要加载storage_state='auth.json',需确保该文件已上传至Colab环境,或先通过Playwright完成登录并保存状态。

内容的提问来源于stack exchange,提问作者Himanshu Poddar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 18:06:31