Phantomjs - take screenshot of a web page(Phantomjs - 截取网页截图)
问题描述
我有一个 URL(例如 http://www.example.com/OtterBox-77-24444-Commuter-Series-Optimus/dp/B00A21KPEI/ref=pd_sim_cps_4
)并且想要截取它并在我的网页上预览.意思是,用户点击预览按钮,PhantomJS 需要将网页预览为 PNG/JPEG
I have a URL (for e.g. http://www.example.com/OtterBox-77-24444-Commuter-Series-Optimus/dp/B00A21KPEI/ref=pd_sim_cps_4
) and want to take a screenshot of it and preview it on my web page. Meaning, the user clicks on the preview button and PhantomJS needs to preview the web page as PNG/JPEG
我也可以使用任何其他开源软件.
I'm ok with using any other open source too.
推荐答案
我假设你已经安装了 Phantomjs 并在你的 .bashrc 中创建了一个别名,或者已经更新了你的系统路径来调用 Phantomjs 二进制文件.如果没有,您需要仔细阅读一些初学者教程:http://net.tutsplus.com/tutorials/javascript-ajax/testing-javascript-with-phantomjs/
I am going to assume you have installed Phantomjs and have created an alias in your .bashrc or have updated your system path to call the Phantomjs binaries. If not, you need to peruse a few beginner tutorials: http://net.tutsplus.com/tutorials/javascript-ajax/testing-javascript-with-phantomjs/
设置完成后,您需要编写一个简单的 javascript 文件,您将在终端(或 shell,如果您使用 Windows)中调用该文件.我将在下面提供一个简单的示例脚本.
Once you have that set up, you will need to write a simple javascript file that you will call in the terminal (or shell, if you are using Windows). I will provide a simple, example script below.
var WebPage = require('webpage');
page = WebPage.create();
page.open('http://google.com');
page.onLoadFinished = function() {
page.render('googleScreenShot' + '.png');
phantom.exit();}
然后,保存您的 js 文件.打开终端或 shell 并运行以下命令
Then, save your js file. Open up your terminal or shell and run the following
phantomjs yourFile.js
就是这样.检查您调用 js 文件的目录,您应该有一个带有网页屏幕截图的 png 文件.
That's it. Check the directory where you called the js file and you should have a png file with a screen shot of your web page.
这非常简单,使用 phantomjs 有很多注意事项,但这是我能做到的最基础的.如果您需要 phantomjs 的其他配方,请尝试查看以下示例脚本:https://github.com/ariya/phantomjs/wiki/示例
This is very simple and there are a lot of caveats to f-ing with phantomjs, but this is about as basic as I can make it. If you need other recipes for phantomjs, try looking at these example scripts: https://github.com/ariya/phantomjs/wiki/Examples
这篇关于Phantomjs - 截取网页截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Phantomjs - 截取网页截图
基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01