我使用pyppeteer在 python 中编写了一个小程序。它在我的 Windows 计算机上运行良好,但是当我尝试在基于 Unix 的系统上运行它时它不起作用。这是一个最小的可重现示例:
import asyncio
from pyppeteer import launch
async def main():
browser = await launch(headless=True)
page = await browser.newPage()
await page.goto('http://www.example.com')
print(await page.content(), flush=True)
asyncio.get_event_loop().run_until_complete(main())
在 Windows 上,它按预期运行并返回一个 HTML 页面。在 Unix 上,大约 35 秒没有发生任何事情后,我收到此错误:
Traceback (most recent call last):
File "pyppeteerTest.py", line 10, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
return future.result()
File "pyppeteerTest.py", line 5, in main
browser = await launch(headless=True)
File "/home/thatcoolcoder/.local/lib/python3.6/site-packages/pyppeteer/launcher.py", line 306, in launch
return await Launcher(options, **kwargs).launch()
File "/home/thatcoolcoder/.local/lib/python3.6/site-packages/pyppeteer/launcher.py", line 167, in launch
self.browserWSEndpoint = get_ws_endpoint(self.url)
File "/home/thatcoolcoder/.local/lib/python3.6/site-packages/pyppeteer/launcher.py", line 226, in get_ws_endpoint
raise BrowserError('Browser closed unexpectedly:\n')
pyppeteer.errors.BrowserError: Browser closed unexpectedly:
我在 Windows 上运行 python 3.8.2,在 Unix 上运行 python 3.6.9,但我怀疑这是问题所在。我已经尝试了这个答案的评论中建议的修复,但这并没有改变任何东西。