![](/img/trans.png)
[英]RuntimeError: There is no current event loop in thread 'Thread-1'. - requests_html, html.render()
[英]How to install chromium in alpine docker to use html.render() from requests_html?
我需要抓取一個 js 渲染的網站。 我發現這個很好的工作庫 requests_html 可以完成這項工作。 使用“pip install requests_html”后,以下代碼將完成工作:
from requests_html import HTMLSession
url = examplesite
session = HTMLSession()
r = session.get(url)
r.html.render(sleep=1)
print(r.html.html)
它第一次運行時會安裝 chromium 以呈現 url。 但是,當我嘗試在 alpine dockerfile 中使用此代碼時,出現以下錯誤:
FileNotFoundError: [Errno 2] No such file or directory: '/root/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome': '/root/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome'
這可能是因為 dockerfile 中不存在根文件夾。 那么如何在 docker 容器中安裝 chromium 呢? 另外,我不限於這個庫,所以如果有更好的可以在 docker 容器中使用的庫,請告訴我。
我已經嘗試了以下方法,但沒有奏效:
FROM python:3.7-alpine3.13
RUN apk add --no-cache chromium --repository=http://dl-cdn.alpinelinux.org/alpine/v3.10/main
我使用不同的容器修復了它:
FROM python:3.8.13-slim-buster
RUN apt-get update
RUN apt install -y gconf-service libasound2 libatk1.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.