惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
爱范儿
爱范儿
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
Y
Y Combinator Blog
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
M
MIT News - Artificial intelligence
博客园 - Franky
V
Visual Studio Blog
I
InfoQ
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
博客园 - 司徒正美
L
LangChain Blog

博客园 - xin478

使用nfs共享session文件导致 session_start异常缓慢问题 nginx proxy_pass末尾神奇的/ 在php中使用SMTP通过密抄批量发送邮件续:修正 无聊闲做,从使用PHP数组实现约瑟夫环问题谈性能 在php中使用SMTP通过密抄批量发送邮件 使用VisualStudio开发php mysql常用的hint[转] php垃圾代码优化那档子事 配置windows下php+apache+mysql环境 惨啊!mp4固件升级惊魂 用PHP实现POP3邮件的收取 希捷酷鱼12 500G的确有问题,好惨啊 解决IE6下PNG透明问题,支持平铺背景图片 VS.PHP with VS2008 in windows7 windows7蓝屏啦 mysql使用rand随机查询记录效率测试 特殊字符的html编码转化 - xin478 - 博客园 asp.net页面事件执行顺序 在visual studio中随意更改中文字体的方法
linux下抓取网页截图
xin478 · 2012-06-06 · via 博客园 - xin478

项目需要抓取指定网址截图的功能,linux下正好有这样的工具,最后选定用phantomjs,这是基于Javascript驱动的命令行webkit引擎,轻量级,安装简单,开发快速,渲染速度较快的无界面的webkit浏览器

更多介绍可以参考 http://code.google.com/p/phantomjs

http://code.google.com/p/phantomjs/downloads/list 下载源码包,我下的是phantomjs-1.5.0-source.tar.gz

# wget http://code.google.com/p/phantomjs/downloads/detail?name=phantomjs-1.5.0-source.tar.gz&can=2&q=

# cd phantomjs-1.5.0/

# ./build.sh

漫长等待后,编译结束。这个不需要在额外安装qt之类的,比较方便。

新建一个js脚本,内容如下:

 
var page = new WebPage(),
    address, output, size;
 
if (phantom.args.length < 2 || phantom.args.length > 3) {
    console.log('Usage: rasterize.js URL filename');
    phantom.exit();} else {
    address = phantom.args[0];
    output = phantom.args[1];
    page.viewportSize = { width: 600, height: 600 };
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
        } else {
            window.setTimeout(function () {
                page.render(output);
                phantom.exit();
            }, 200);
        }
    });}

保存为snap.js

需要截图的时候,执行如下命令:

# ./bin/phantomjs snap.js http://qq.com qq.com.png

即生成了截图文件 qq.com.png。

目前,phantomjs还是需要x-server的依赖才可以运行的,因此需要安装xfvb。