【发布时间】:2020-12-20 17:51:22
【问题描述】:
我正在使用堆栈:
- 硒
- 蟒蛇
- 码头工人
用于网页抓取。
Dockerfile 看起来像:
FROM python:3.7
# download chromedriver
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \
apt-get -y update && \
apt-get install -y google-chrome-stable && \
# unzip chromedriver
apt-get install -yqq unzip && \
wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip && \
unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
WORKDIR /src
COPY requirements.txt /src/requirements.txt
RUN pip install -r /src/requirements.txt
COPY src /src
ENTRYPOINT ["python", "main.py"]
在 docker 容器中,我的代码如下所示:
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
DRIVER_PATH = "/usr/local/bin/chromedriver"
URL = "google.com"
SLEEP_TIME = 60
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
while True:
browser = webdriver.Chrome(executable_path=DRIVER_PATH, options=options)
browser.get(URL)
browser.quit()
sleep(SLEEP_TIME)
但是 chromdriver 开始创建僵尸进程:
所有这些进程都是由 chrome 驱动程序从主进程创建的(我现在不知道什么是“猫”):
有没有办法正确终止 webdriver?
附:我知道我只能使用一个 webdriver 实例,但是关于关闭 webdriver 的问题仍然存在。
【问题讨论】:
标签: python docker selenium selenium-webdriver selenium-chromedriver