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的操作,感覺用起來很舒服。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章