Python-pyppeteer-chrome报错解决:ad NaCl helper startup ack (0 bytes)

此文首发于我的个人博客:zhang0peter的个人博客


在使用pyppeteer操作Chrome进行爬虫时,大约过了1个多小时会报错如下:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/pyppeteer/launcher.py", line 330, in launch
    return await Launcher(options, **kwargs).launch()
  File "/usr/local/lib/python3.6/dist-packages/pyppeteer/launcher.py", line 174, in launch
    self.browserWSEndpoint = self._get_ws_endpoint()
  File "/usr/local/lib/python3.6/dist-packages/pyppeteer/launcher.py", line 219, in _get_ws_endpoint
    self.proc.stdout.read().decode()
pyppeteer.errors.BrowserError: Browser closed unexpectedly:
LaunchProcess: failed to execvp:
/root/.local/share/pyppeteer/local-chromium/588429/chrome-linux/nacl_helper
[6122:6122:0107/212909.165245:ERROR:nacl_fork_delegate_linux.cc(314)] Bad NaCl helper startup ack (0 bytes)
Xlib:  extension "XInputExtension" missing on display ":1.0".
Xlib:  extension "RANDR" missing on display ":1.0".
Xlib:  extension "XInputExtension" missing on display ":1.0".
Xlib:  extension "XInputExtension" missing on display ":1.0".
Created new window in existing browser session.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "youtube-search-spider.py", line 138, in <module>
    asyncio.get_event_loop().run_until_complete(new_search_page(url=search_url))
  File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
    return future.result()
  File "/usr/lib/python3.6/asyncio/tasks.py", line 180, in _step
    result = coro.send(None)
  File "youtube-search-spider.py", line 93, in new_search_page
    await browser.close()
UnboundLocalError: local variable 'browser' referenced before assignment

这个报错我一直没找到原因,我猜测是因为我使用Linux服务器开远程桌面进行爬虫,然后远程桌面可能有问题导致了这个报错。

我的解决方法是不使用pyppeteer,改为谷歌原生的puppeteer,用nodejs重写一遍爬虫。原生的puppeteer操作无头浏览器不会报错,而魔改后的pyppeteer是会报错的。