requeests_html报错pyppeteer.errors.BrowserError: Browser closed unexpectedly:解决办法

这是关于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的操作,感觉用起来很舒服。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值