Python 将 URL 转换为屏幕截图(脚本)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3811674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert URL to screenshot (script)
提问by Kalinin
There is the URL of page on the Internet. I need to get a screenshot of this page (no matter in which browser).
互联网上有页面的URL。我需要获取此页面的屏幕截图(无论在哪个浏览器中)。
I need a script (PHP, Python (even Django framework)) that receives the URL (string) and output screenshot-file at the exit (file gif, png, jpg).
我需要一个脚本(PHP、Python(甚至是 Django 框架))接收 URL(字符串)并在出口处输出屏幕截图文件(文件 gif、png、jpg)。
UPD:
更新:
I need dynamically create a page where opposite to URL will be placed screenshot of the page with the same URL.
我需要动态创建一个页面,其中与 URL 相对的页面将放置具有相同 URL 的页面的屏幕截图。
采纳答案by CristiC
Why do you need a script when you can use a service from another site?
Check for example what I am using: WebSnapr http://www.websnapr.com/
Or check http://www.google.ro/search?ie=UTF-8&q=website+thumbnailif something else fits your request.
当您可以使用其他站点的服务时,为什么还需要脚本?
例如检查我正在使用的内容:WebSnapr http://www.websnapr.com/
或者检查http://www.google.ro/search?ie=UTF-8&q=website+thumbnail如果其他内容符合您的要求。
回答by Shikiryu
You can use, as I do, the shotbox API
你可以像我一样使用 shotbox API
It's in french, but still, quickly:
它是法语,但仍然很快:
- Use h t t p://add.shotbot.net/k=key/urlwhere keyis your API key and url... the page you want as a screenshot
- Use h t t p://static.shotbot.net/md5url/format.jpg or h t t p://cache.shotbot.net/s=format/urlwhere formatcan be 80(80x60), 92(92x69), 120(ascreen 120x90), 160(160x120), 240(240x180), 320(320x240), 1024(1024x768)
- 使用 http://add.shotbot.net/k= key/url其中key是您的 API 密钥和url...您想要作为屏幕截图的页面
- 使用http://static.shotbot.net/ md5url /格式.jpg或http://cache.shotbot.net/s=格式/ URL,其中格式可以是80(80x60),92(92x69),120(ascreen 120x90 )、160(160x120)、240(240x180)、320(320x240)、1024(1024x768)
To get your API key : http://translate.google.fr/translate?hl=fr&sl=fr&tl=en&u=http%3A%2F%2Fwww.shotbot.net%2Fcreer-un-compte-webmaster.php
要获取您的 API 密钥:http: //translate.google.fr/translate?hl=fr&sl=fr&tl=en&u=http%3A%2F%2Fwww.shotbot.net%2Fcreer-un-compte-webmaster.php
回答by Mee
<img src='http://zenithwebtechnologies.com.au/thumbnail.php?url=www.subway.com.au'>
Pass the url as argument and you'll get the image for more details check this link http://zenithwebtechnologies.com.au/auto-thumbnail-generation-from-url.html
将 url 作为参数传递,您将获得图像以获取更多详细信息,请查看此链接http://zenithwebtechnologies.com.au/auto-thumbnail-generation-from-url.html
回答by SoCo
There is also http://webshot.mine.sk/free with no limits, multiple APIs (js, jquery, php)
还有http://webshot.mine.sk/免费,没有限制,多个 APIs (js, jquery, php)
回答by Jin Nguyen
If you are family with Python, you can use PyQt4. This library supports to get screenshot from a url.
如果你是 Python 的家人,你可以使用 PyQt4。该库支持从 url 获取屏幕截图。
回答by Ashish Gupta
PhantomJSis a better option for generating screenshot from URL. The following script demonstrates the simplest use of page capture. It loads the Github homepage and then saves it as an image, github.png. Code
PhantomJS是从 URL 生成屏幕截图的更好选择。以下脚本演示了页面捕获的最简单用法。它加载 Github 主页,然后将其保存为图像 github.png。代码
var page = require('webpage').create();
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});
To run this example create a new file called github.js. Copy and paste the above code into the github.js file. In the commandline, run this newly created script with PhantomJS:
要运行此示例,请创建一个名为 github.js 的新文件。将上述代码复制并粘贴到 github.js 文件中。在命令行中,使用 PhantomJS 运行这个新创建的脚本:
phantomjs github.js
There a lot of projectsfor generating screenshots using PhantomJS. Pageresgenerates reliable screenshots and its based on NodeJS and PhantomJS.
有很多使用 PhantomJS 生成屏幕截图的项目。 Pageres基于NodeJS和 PhantomJS 生成可靠的屏幕截图。
回答by Ashish Gupta
Solution using Google Page Speed - tested & working.
使用 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.
*/
?>

