Requests-html 导致 OSError: [Errno 8] 调用 html.render() 时执行格式错误

tgr*_*af2 6 python python-3.x python-requests python-requests-html

我正在使用 requests-html 并尝试渲染功能,但收效甚微。当我使用 python3.8 运行这个脚本时

#!/usr/bin/python3
from requests_html import HTML
file = "scrape/temp_file2.html"
with open(file) as html_file:
   source = html_file.read()
   html = HTML(html=source)
   html.render()
   match = html.find('#footer', first=True)
   try:
      print(match.hmtl)
   except:
      print('not found')
Run Code Online (Sandbox Code Playgroud)

它会导致回溯:

python3 scrape/test1.py
Traceback (most recent call last):
  File "scrape/test1.py", line 10, in <module>
    html.render()
  File "/home/pi/.local/lib/python3.8/site-packages/requests_html.py", line 586, in render
    self.browser = self.session.browser  # Automatically create a event loop and browser
  File "/home/pi/.local/lib/python3.8/site-packages/requests_html.py", line 730, in browser
    self._browser = self.loop.run_until_complete(super().browser)
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/pi/.local/lib/python3.8/site-packages/requests_html.py", line 714, in browser
    self._browser = await pyppeteer.launch(ignoreHTTPSErrors=not(self.verify), headless=True, args=self.__browser_args)
  File "/home/pi/.local/lib/python3.8/site-packages/pyppeteer/launcher.py", line 306, in launch
    return await Launcher(options, **kwargs).launch()
  File "/home/pi/.local/lib/python3.8/site-packages/pyppeteer/launcher.py", line 147, in launch
    self.proc = subprocess.Popen(  # type: ignore
  File "/usr/local/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/home/pi/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome'
Run Code Online (Sandbox Code Playgroud)

chromium 文件是一个可执行文件:

ls -l /home/pi/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome
-rwxr-xr-x 1 pi pi 214121928 Mar  9 17:21 /home/pi/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome
Run Code Online (Sandbox Code Playgroud)

如果我删除 html.render() 行,它可以正常工作(但不会渲染 javascript)。有任何想法吗?

tgr*_*af2 9

经过多次搜索我找到了解决方案。问题是 pyppeteer 在 32 位 ARM 处理器上安装 x86-64 版本的 chromium。下面的解决方案是从 Ubuntu 安装 chromium 文件并从 pyppeteer 目录链接到这些文件。来自https://github.com/miyakogi/pyppeteer/issues/250

\n
install chromium-codecs-ffmpeg:\n   https://launchpad.net/ubuntu/trusty/armhf/chromium-codecs-ffmpeg\n   \xe2\x9e\x9c chromium-codecs-ffmpeg_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb\n   sudo dpkg --force-all -i chromium-codecs-ffmpeg_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb\n\ninstall chromium-browser:\n   https://launchpad.net/ubuntu/trusty/armhf/chromium-browser\n   \xe2\x9e\x9c chromium-browser_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb\n   sudo dpkg --force-all -i chromium-browser_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb\n
Run Code Online (Sandbox Code Playgroud)\n

其他注意事项:

\n
    \n
  • 确保您下载的版本 >= 62。我使用过 65 版本并且工作正常。
  • \n
  • 首先安装 chromium-codecs-ffmpeg,然后使用命令\n“dpkg --force-all -i”安装 chromium-browser。
  • \n
  • 从 Ubuntu 软件包安装 Chromium 后,通过运行“whereis chromium-browser”获取其路径
  • \n
  • 最有可能的是,它会位于“/usr/bin/chromium-browser”上。
  • \n
  • 进入 pyppeteer 下载 x86 Chromium 的目录。
  • \n
  • 在你的情况下它是“/home/pi/.local/share/pyppeteer/local-chromium/588429/chrome-linux/”。
  • \n
  • 删除其所有文件。
  • \n
  • 在同一目录中。创建一个名为“chrome”的符号链接并指向 Ubuntu 的 Chromium(您已在步骤 3 中获得了其路径)。
  • \n
  • 例如: ln -s /usr/bin/chromium-browser chrome
  • \n
  • 执行这些步骤后,一切都应该正常。
  • \n
\n

  • 如果 Raspberry Pi 上已安装 Chromium,您只需链接到现有安装即可。 (2认同)