这是关于requests_html调用pyppeteer获取js渲染后页面的报错。
代码如下:
from requests_html import HTMLSession
session = HTMLSession()
res = session.get("http://sec.didichuxing.com/present")
res.html.render() # 动态渲染页面
print(res.html.html) # 输出渲染之后的页面
- 但是对我而言上面这段代码运行直接报错了!
- 环境: centos 7 + python 3.6
原因:
很大部分原因是环境问题
解决办法:
- 1 首先安装依赖, 注意我的是centos 7系统,至少以下方式对我有效
sudo yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc
- 2 更新 nss 库
yum update nss -y
以上两个步骤至少解决了我的问题,至于 Ubuntu 系统可安装如下
sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
- 最后感谢这位老哥的博客
https://thief.one/2019/08/16/2/
然后我找解决办法时,对此问题有帮助的链接如下:
1 这是requests_html的 issue,kennethreitz说问题出在 pyppeteer
https://github.com/psf/requests-html/issues/52
2 这是上面问题中关于 pyppeteer 的解决办法。里面有centos ubuntu等等各种环境解决办法
https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
- 之前都是使用 selenium 之类的获取js渲染的页面,现在发现 requests_html 封装了pyppeteer的操作,感觉用起来很舒服。