网页使用phantomjs lib捕获并保存到图像 [英] Web page Capture and save to image using phantomjs lib

查看:143
本文介绍了网页使用phantomjs lib捕获并保存到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索谷歌获取任何js lib,可以捕获任何网站或网址的图像。我开始知道phantomjs库可以做到这一点。在这里我有一个小代码捕获并转换github主页到png图像

i was searching google to get any js lib which can capture the image of any website or url. i came to know that phantomjs library can do it. here i got a small code which capture and convert the github home page to png image

如果有人熟悉phantomjs那么请告诉我这行的含义是什么

if anyone familiar with phantomjs then please tell me what is the meaning of this line

var page = require('webpage').create();

在这里我可以提供任何名称而不是网页?

here i can give any name instead of webpage ?

如果我需要捕获任何网页的部分,那么我该如何在这个库的帮助下完成它。任何人都可以指导我。

if i need to capture the portion of any webpage then how can i do it with the help of this library. anyone can guide me.

var page = require('webpage').create();
page.open('http://github.com/', function () {
    page.render('github.png');
    phantom.exit();
});

https://github.com/ariya/phantomjs/wiki

谢谢

推荐答案

你问的问题:

var page = require('webpage').create();

据我所知,该行有3件事:它增加了一个模块 require('webpage'),然后在PhantomJS .create()中创建一个WebPage对象,然后将该Object分配给 var = page

As far as I can tell, that line does 3 things: It adds a module require('webpage'), then creates a WebPage Object in PhantomJS .create(), and then assigns that Object to var = page

名称网页告诉它要添加哪个模块。

The name "webpage" tells it which module to add.

http://phantomjs.org/api/webpage/

我也需要一种方法来使用 page.render()来捕获网页的一个部分,但我不知道看到一个简单的方法来做到这一点。按ID选择页面元素并根据它的大小呈现该元素会很好。他们应该为PhantomJS的下一个版本添加它。

I too need a way to use page.render() to capture just one section of a web page, but I don't see an easy way to do this. It would be nice to select a page element by ID and just render out that element based at whatever size it is. They should really add that for the next version of PhantomJS.

现在,我唯一的解决方法是在我的URL http中添加一个锚标记: //example.com/page.html#element 使页面滚动到我想要的元素,然后设置一个接近我需要的大小的宽度和高度。

For now, my only workaround is to add an anchor tag to my URL http://example.com/page.html#element to make the page scroll to the element that I want, and then set a width and height that gets close to the size I need.

我最近发现在渲染之前我可以稍微操纵页面,所以我想尝试使用这种技术来隐藏除了我想要捕获的其他元素之外的所有其他元素。我还没试过,但也许我会取得一些成功。
查看本页并查看他们如何使用 querySelector() https://github.com/ariya/phantomjs/blob/master/examples/technews.js

I recently discovered that I can manipulate the page somewhat before rendering, so I want to try to use this technique to hide all of the other elements except the one I want to capture. I have not tried this yet, but maybe I will have some success. See this page and look at how they use querySelector(): https://github.com/ariya/phantomjs/blob/master/examples/technews.js

这篇关于网页使用phantomjs lib捕获并保存到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆