FastAPI中使用Playwright注意点

Playwright可以直接在单文件中执行,使用sync_plywright就行,但有时我们想让它在框架中运行,再通过API去调用它,这时就会遇到一些问题。

有一些点要注意下:

  1. 要使用async_playwright()而不是sync_playwright(),而且要在方法前加async关键字,所有playwright方法执行前加await关键字,包括对封装了playwright方法的调用。

不然会报如下报错:

1. It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead.
2. PlaywrightContextManager' object does not support the context manager protocol
  1. uvicorn运行时不要加reload,不然会报NotImplementedError错误。

Leave a Comment

豫ICP备19001387号-1