如何在 Ubuntu 上设置和运行 PhantomJS?

2024-10-17 08:47:00
admin
原创
304
摘要:问题描述:我设置了 PhantomJS 并将其录制到视频:https://www.dailymotion.com/video/xnizmh_1_webcam构建说明:http ://phantomjs.org/build.html我的设置有什么问题吗?设置完成后,我阅读了快速入门教程并尝试编写此代码phant...

问题描述:

我设置了 PhantomJS 并将其录制到视频:https://www.dailymotion.com/video/xnizmh_1_webcam

构建说明:http ://phantomjs.org/build.html

我的设置有什么问题吗?

设置完成后,我阅读了快速入门教程并尝试编写此代码

phantomjs hello.js 

它显示“未找到命令”错误。我该如何解决这个问题?


解决方案 1:

Guidouil 的回答让我走上了正确的道路。我必须向 /usr/bin/ 添加一个额外的符号链接,并且我为所有 3 个都创建了直接符号链接 - 见下文。

我正在 Ubuntu 服务器 Natty Narwhal 上安装。

这正是我所做的。

cd /usr/local/share
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo tar xjf phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/bin/phantomjs

最后当我

phantomjs -v

我得到 1.9.7

如果有人发现我所做的事情有任何问题,请告诉我。

解决方案 2:

PhantomJS 在npm上。你可以运行以下命令来全局安装它:

npm install -g phantomjs-prebuilt  

phantomjs -v应该返回2.1.1

解决方案 3:

从 phantomjs 网站下载预构建包:
http://phantomjs.org/download.html
然后打开终端并转到下载文件夹

sudo mv phantomjs-1.8.1-linux-x86_64.tar.bz2 /usr/local/share/.
cd /usr/local/share/
sudo tar xjf phantomjs-1.8.1-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.8.1-linux-x86_64 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin/phantomjs

然后检查安装phantomjs -v应该返回1.8.1

解决方案 4:

从包管理器安装:

sudo apt-get install phantomjs

解决方案 5:

下面是我使用的构建步骤(请注意,这些说明适用于 1.3 版。有关最新 PhantomJS 的安装说明,请参阅此答案的评论):

sudo apt-get update
sudo apt-get install git-core
sudo apt-get install build-essential
sudo apt-get install libqt4-dev libqtwebkit-dev qt4-qmake
git clone git://github.com/ariya/phantomjs.git && cd phantomjs
git checkout 1.3
qmake-qt4 && make

现在安装 Xvfb

sudo apt-get install xvfb xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic

启动 Xvfb:

Xvfb :23 -screen 0 1024x768x24 &

现在运行 phantom:

DISPLAY=:23 ./phantomjs hello.js

解决方案 6:

对于 PhantomJS 1.5 以上的版本,请考虑这一点(Phantom 网站上的构建说明的逐字副本):

对于 Ubuntu Linux(在 Ubuntu 10.04 Lucid Lynx 和 Ubuntu 11.04 Natty Narwhal 的准系统安装上进行测试):

sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.7
./build.sh

解决方案 7:

对于 Ubuntu,您可以使用从 PhantomJS 网站下载的预构建版本。

如果您有大量时间,您也可以自己构建它。 (这正是 Nikhil 的回答中的程序)。

PhantomJS 的伙计们建议使用二进制文件来节省时间:

警告:从源代码编译 PhantomJS 需要很长时间,主要是因为 WebKit 模块中有数千个文件。在现代机器上有 4 个并行编译作业,整个过程大约需要 30 分钟。如果有现成的二进制包,强烈建议下载并安装。

我认为,对于现代机器来说,它们的意思是 > 4 个内核,> 8GB 内存。我在一个微型 AWS 实例上尝试过,2 小时后就放弃了。

简而言之:按照 PhantomJS 站点的说明安装预构建的包。

解决方案 8:

我个人更喜欢使用npm(参见Arnel Bucio 的回答)

sudo npm install -g phantomjs

但是!我注意到一些npm模块仍然无法将其视为全局可执行文件。

在此处输入图片描述

所以!

  • 创建新的/usr/share/phantomjs/目录链接

cd /usr/share
sudo ln -s ../lib/node_modules/phantomjs/lib/phantom phantomjs
  • 删除旧的/usr/bin/phantomjs可执行链接并创建新的可执行链接

cd /usr/bin
sudo mv phantomjs phantomjs.old
sudo ln -s ../share/phantomjs .

解决方案 9:

在我的流浪引导程序中:

apt-get install -y build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
echo y | ./build.sh
ln -s /home/vagrant/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
cd ..

解决方案 10:

我已经做完了这个。

sudo apt-get update
sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
./build.sh

解决方案 11:

请参阅链接安装指南位于...

https://gist.github.com/julionc/7476620

并使用此命令在终端中运行

phantomjs --webdriver=4444

解决方案 12:

来自官方网站:phantomjs site

sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.8
./build.sh

解决方案 13:

对于 Ubuntu,从http://phantomjs.org/download.html下载合适的文件。CD 到下载的文件夹。然后:

sudo tar xvf phantomjs-1.9.0-linux-x86_64.tar.bz2
sudo mv phantomjs-1.9.0-linux-x86_64 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/bin/phantomjs

确保将这些命令中的文件名替换为您下载的文件。

解决方案 14:

请注意,这绝对是实现此目的的一种方法:

$ sudo apt-get install phantomjs
$ phantomjs -v
1.6.0

遗憾的是,它安装的是 1.6 版而不是最新版本,但这对我来说是可行的。

解决方案 15:

我知道这太老了,但是,万一有人现在从 Google 上看到这个问题,你可以通过输入 apt-get install phantomjs 来安装它

解决方案 16:

在适用于 Windows 的 Ubuntu 上,我发现apt-get这两个版本都不适用于我。起作用的是来自此评论的npm脚本。

为了方便使用,我将整个内容粘贴到名为的脚本文件中install_phantomjs.sh,使其可执行(chmod u+x install_phantomjs.sh),然后运行它(./install_phantomjs.sh

解决方案 17:

或者最新的32位版本Linux

sudo wget http://phantomjs.googlecode.com/files/phantomjs-1.9.2-linux-i686.tar.bz2

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/local/share/phantomjs

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/local/bin/phantomjs

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/bin/phantomjs

解决方案 18:

安装并调用Phantomjs

按照步骤操作无效,但可以从其他人那里克隆。(ver2.0)

解决方案 19:

以下是 Julio Napurí 的安装过程https://gist.github.com/julionc

版本:1.9.8

平台:x86_64

首先,安装或更新到最新的系统软件。

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev

安装 PhantomJS 正常工作所需的这些包。

sudo apt-get install libfreetype6 libfreetype6-dev
sudo apt-get install libfontconfig1 libfontconfig1-dev

从 PhantomJS 网站获取它。

cd ~
export PHANTOM_JS="phantomjs-1.9.8-linux-x86_64"
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2

下载后,将 Phantomjs 文件夹移动到 /usr/local/share/ 并创建符号链接:

sudo mv $PHANTOM_JS /usr/local/share
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin

现在,您的系统上应该已经正确拥有 PhantomJS。

phantomjs --version

解决方案 20:

我找到了这种更简单的方法 - Phantom 依赖项 + Npm

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev
sudo apt-get install libfreetype6 libfreetype6-dev
sudo apt-get install libfontconfig1 libfontconfig1-dev

和 npm

[sudo] npm install -g phantomjs

完毕。

解决方案 21:

在Linux上运行hello.js不要忘记添加hello.js的路径:

phantomjs YourPathToPhantomjsFolder/examples/hello.js

解决方案 22:

如果你想轻松使用 phantomjs,你可以在 phantomjscloud.com 上使用它,只需通过 http 请求即可获得结果。

解决方案 23:

这就是我在 docker 容器上的 /usr/local/bin 中放置特定版本的 phantomjs 的方法。

curl -Ls https://github.com/Medium/phantomjs/releases/download/v1.9.19/phantomjs-1.9.8-linux-x86_64.tar.bz2 \n    | tar jxvf - --strip-components=2 -C /usr/local/bin/ ./phantomjs-1.9.8-linux-x86_64/bin/phantomjs

或不./依赖于操作系统。

curl -Ls https://github.com/Medium/phantomjs/releases/download/v1.9.19/phantomjs-1.9.8-linux-x86_64.tar.bz2 \n    | tar jxvf - --strip-components=2 -C /usr/local/bin/ phantomjs-1.9.8-linux-x86_64/bin/phantomjs

解决方案 24:

sudo您无需或即可启动并运行npm。只需下载提取添加到路径即可。

如果您习惯备份整个主文件夹,那么这个方法还有一个额外的好处,那就是备份起来非常方便,我强烈推荐您这样做。这个方法也适用于任何版本的 Linux。

➤  cd ~
➤  wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
➤  mkdir phantomjs
➤  tar xjf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C phantomjs
➤  echo 'export PATH="$PATH:$HOME/phantomjs/bin"' >> .profile
➤  source .profile
➤  phantomjs -v
2.1.1

缺点是:

  1. 您需要手动升级

  2. 其他用户将无权访问此内容。

你可以使用非常简单的 shell 脚本来安装/升级

#!/bin/sh
# install_phantomjs.sh $VERSION

$VERSION = $1
printf "Downloading PhantomJS $VERSION...
"
wget "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$VERSION-linux-x86_64.tar.bz2"

printf "Extracting PhantomJS $VERSION to ~/phantomjs...
"
mkdir ~/phantomjs
tar xjf phantomjs-$VERSION-linux-x86_64.tar.bz2 -C ~/phantomjs

printf "Done! Make sure $HOME/phantomjs/bin is in your path.
"

或者在 Dockerfile 中

# Download and setup PhantomJS
ENV PHANTOMJS_VERSION 2.1.1
RUN curl -fSL "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2" -o /tmp/phantomjs.tar.bz2 && \n  mkdir ~/phantomjs && \n  tar xjf /tmp/phantomjs.tar.bz2 -C ~/phantomjs && \n  rm /tmp/phantomjs.tar.bz2
ENV PATH /home/$USERNAME/phantomjs/bin:$PATH

解决方案 25:

这是我在 ubuntu 16.04 机器上所做的

sudo apt-get update
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo mv path/where/you/downloaded/phantomjs  /usr/bin

最后当我

phantomjs -v

我得到2.1.1

看完这个帖子的每个答案后。我认为这是在 ubuntu 中安装和运行 phantomjs 的最佳解决方案。

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   2357  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1509  
  PLM(产品生命周期管理)系统在企业项目管理中扮演着至关重要的角色,它能够整合产品从概念设计到退役的全流程信息,提升协同效率,降低成本。然而,项目范围蔓延是项目管理过程中常见且棘手的问题,在PLM系统环境下也不例外。范围蔓延可能导致项目进度延迟、成本超支、质量下降等一系列不良后果,严重影响项目的成功交付。因此,如何在P...
plm项目经理是做什么   1  
  PLM(产品生命周期管理)系统在现代企业的产品研发与管理过程中扮演着至关重要的角色。它不仅仅是一个管理产品数据的工具,更能在利益相关者分析以及沟通矩阵设计方面提供强大的支持。通过合理运用PLM系统,企业能够更好地识别、理解和管理与产品相关的各类利益相关者,构建高效的沟通机制,从而提升产品开发的效率与质量,增强企业的市场...
plm是什么   2  
  PLM(产品生命周期管理)项目管理对于企业产品的全生命周期规划、执行与监控至关重要。在项目推进过程中,监控进度偏差是确保项目按时、按质量完成的关键环节。五维健康检查指标体系为有效监控PLM项目进度偏差提供了全面且系统的方法,涵盖了项目的多个关键维度,有助于及时发现问题并采取针对性措施。需求维度:精准把握项目基石需求维度...
plm项目管理软件   2  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用