将URL转换为屏幕截图(脚本)

7

这是一个互联网页面的URL。我需要获取此页面的屏幕截图(无论在哪个浏览器中)。

我需要一个脚本(PHP、Python(甚至Django框架)),它接收URL(字符串)并在退出时输出屏幕截图文件(文件gif、png、jpg)。

更新:

我需要动态创建一个页面,在该页面的对面将放置具有相同URL的页面的屏幕截图。


PHP和Python仅仅是文本处理引擎,而不是图像渲染引擎。真遗憾。 - Your Common Sense
6个回答

6

我需要动态创建一个页面,在该页面的对面位置放置与URL相同的页面截图。 - Kalinin
1
这些服务通常会返回相应URL的缩略图。您可以在后台请求此类作业(或在保存数据时),并将其存储在数据库中(以便以后重用),或者只是显示它(我会选择第一种方法)。 - CristiC
这个能自动完成吗(我大约有1000个网站(URL))? - Kalinin
1
你是指批量吗?选择一个服务,如果需要注册,然后使用循环遍历所有的URL,发出服务请求并存储生成的缩略图。 - CristiC
你可以任意次数运行一个脚本,而无需支付任何费用。没有第三方在脚本中聚合元数据。即使服务停止(例如websnapr.com),脚本仍然能够工作多年。使用脚本而非服务有许多原因。这似乎是一种相当年轻但实用的工具:https://github.com/maaaaz/webscreenshot - Tomislav Nakic-Alfirevic

2
PhantomJS是从URL生成截图的更好选择。 以下脚本演示了最简单的页面截图用法。它加载Github首页,然后将其保存为图像github.png。 代码:
var page = require('webpage').create();
page.open('http://github.com/', function() {
  page.render('github.png');
  phantom.exit();
});

运行此示例,请创建一个名为github.js的新文件。将上面的代码复制并粘贴到github.js文件中。在命令行中,使用PhantomJS运行这个新创建的脚本:
phantomjs github.js

有很多使用PhantomJS生成截图的项目Pageres生成可靠的截图,基于NodeJS和PhantomJS。

在Linux上使用相同的代码但无法工作,您能否指定需要进行哪些更改才能使其在Linux服务器上正常工作。 - zulfi
@zulfi 它应该在Linux上运行。什么没有工作?尝试Pageres,它很简单。 - Ashish Gupta

1
使用Google Page Speed的解决方案-经过测试并且可行。
//SOLUTION 1

<?php
$link = "http://example.com";
$googlePagespeedData = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$link&screenshot=true");
$googlePagespeedData = json_decode($googlePagespeedData, true);
$screenshot = $googlePagespeedData['screenshot']['data'];
$screenshot = str_replace(array('_','-'),array('/','+'),$screenshot); 
$show_link = "<a href='$link'><img src=\"data:image/jpeg;base64,".$screenshot."\" /></a>";
echo $show_link;

//SOLUTION 2

$name = 'test';
$googlePagespeedData = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$link&screenshot=true");
$googlePagespeedData = json_decode($googlePagespeedData, true);
$screenshot = base64_decode($googlePagespeedData['screenshot']['data']);
$data = str_replace('_','/',$googlePagespeedData['screenshot']['data']);
$data = str_replace('-','+',$data);
$decoded = base64_decode($data);
file_put_contents('myfolder/'.$name.'.jpg',$decoded);
$file_name = "$name.jpg";

/*
-- IMPORTANT INFORMATION -- READ BELOW --

Choose how to proceed!
1. Use the above to display screenshots of links = longer processing time for multiple links.
2. Save image to a file, reference the saved image = more disk space needed if multiple links.

Note the trade off between processing time and disk space, if you're on a shared hosting platform with a small disk space limit and envisage or already have a lot of users (forums beware) you may want to consider a bigger hosting plan or even a dedicated server.

*/
?>

0

网站已经不存在了。 - Antoine Driard

0
如果您熟悉Python,可以使用PyQt4。该库支持从URL获取屏幕截图。

0

你可以像我一样使用Shotbox API

这是法语,但是很快就能明白:

  • 使用h t t p://add.shotbot.net/k=密钥/网址,其中密钥是你的API密钥,网址是你要截图的页面。
  • 使用 h t t p://static.shotbot.net/md5网址/格式.jpg 或 h t t p://cache.shotbot.net/s=格式/网址 ,其中格式可以是 80(80x60), 92(92x69), 120(ascreen 120x90),160(160x120),240(240x180),320(320x240),1024(1024x768)。

获取您的API密钥:http://translate.google.fr/translate?hl=fr&sl=fr&tl=en&u=http%3A%2F%2Fwww.shotbot.net%2Fcreer-un-compte-webmaster.php


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