如何在Scrapinghub上安装xvfb以使用Selenium?

3
我在我的爬虫(Scrapy)中使用Python-Selenium,为了使用Selenium,我需要在Scrapinghub上安装Xvfb。
当我使用apt-get安装Xvfb时,我遇到了以下错误信息:
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)                                                                                                                                                                
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

有没有其他方法在Scrapinghub上安装xvfb?

更新1

我阅读了这个,我尝试使用docker,在这个阶段卡住了。

shub-image init --requirements path/to/requirements.txt

我读到了这个信息:

如果您在运行 shub-image init 时遇到像这样的 ImportError: 您应该通过运行以下命令确保您安装了最新版本的 shub:

$ pip install shub --upgrade

但是我一直都有这个错误。

Traceback (most recent call last):
  File "/usr/local/bin/shub-image", line 7, in <module>
    from shub_image.tool import cli
  File "/usr/local/lib/python2.7/dist-packages/shub_image/tool.py", line 42, in <module>
    command_module = importlib.import_module(module_path)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/dist-packages/shub_image/push.py", line 4, in <module>
    from shub.deploy import list_targets
ImportError: cannot import name list_targets
2个回答

3

你尝试过以下方法吗:

sudo apt-get install xvfb

另一种方法是手动编译软件包,类似于:
apt-get source xvfb
./configure --prefix=$HOME/myapps
make
make install

第三种方法是从源网页https://pkgs.org/download/xvfb下载.deb文件。

下载完成后,您可以将其移动到已下载源代码的路径下:

mv xvfb_1.16.4-1_amd64.deb /var/cache/apt/archives/

然后你改变你的目录并执行:

sudo dpkg -i xvfb_1.16.4-1_amd64.deb

而这就是全部内容!


在Scrapinghub上,我们不能使用sudo,因为我们不是root用户。 - parik
@parik 好的,我更新了答案,希望有所帮助,请告诉我。 - developer_hatch
谢谢你的回答,Damian。能否将它放在setup.py中? - parik
@parik 当然,看这个 https://www.pydanny.com/python-dot-py-tricks.html,很高兴能帮助 :) - developer_hatch
谢谢,我认为最好尝试Docker,因为在Scrapinghub上,我不知道该放什么来代替./configure --prefix=$HOME/myapps。我会尝试使用Docker,并等待Scrapinghub的某个人回答。 - parik

1
I resolved my problems (use selenium in scrapinghub)
1- For xvfb in docker, I use
RUN apt-get install -qy xvfb

2- 创建Docker镜像时,我使用了this,而安装geckodriver则使用了以下代码

#
# Geckodriver Dockerfile
#

FROM blueimp/basedriver

# Add the Firefox release channel of the Debian Mozilla team:
RUN echo 'deb http://mozilla.debian.net/ jessie-backports firefox-release' >> \
  /etc/apt/sources.list \
  && curl -sL https://mozilla.debian.net/archive.asc | apt-key add -

# Install Firefox:
RUN export DEBIAN_FRONTEND=noninteractive \
  && apt-get update \
  && apt-get install --no-install-recommends --no-install-suggests -y \
    firefox \
  # Remove obsolete files:
  && apt-get clean \
  && rm -rf \
    /tmp/* \
    /usr/share/doc/* \
    /var/cache/* \
    /var/lib/apt/lists/* \
    /var/tmp/*

# Install geckodriver:
RUN export BASE_URL=https://github.com/mozilla/geckodriver/releases/download \
  && export VERSION=$(curl -sL \
    https://api.github.com/repos/mozilla/geckodriver/releases/latest | \
    grep tag_name | cut -d '"' -f 4) \
  && curl -sL \
  $BASE_URL/$VERSION/geckodriver-$VERSION-linux64.tar.gz | tar -xz \
  && mv geckodriver /usr/local/bin/geckodriver

USER webdriver

CMD ["geckodriver", "--host", "0.0.0.0"]

来自这里


嗨parik!我对上一个答案进行了另一个更新,所以现在有两个解决问题的选项。很高兴你之前能找到一种方法,抱歉我不能更早地回答 :/ - developer_hatch
@DamianLattenero 谢谢,你的答案帮了我很多,我没有接受它,因为它没有涵盖问题的所有步骤,再次感谢。 - parik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接