截屏大师

介绍

网页截屏大师使用真正的Chrome浏览器捕捉像素完美的屏幕截图,我们的核心服务托管在阿里云与腾讯云之上,API天然分布式、高可用。

起步

您可以使用 GET 或者 POST 向下面的入口发起一个网页截屏请求。

https://www.screenshotmaster.com/api/v1/screenshot

例子

如果要获取百度首页的图片 您可以将 baidu.com 添加到 url 参数中以发起请求。

https://www.screenshotmaster.com/api/v1/screenshot?url=baidu.com

认证

添加 token 参数在您的API请求中以获得认证。

https://www.screenshotmaster.com/api/v1/screenshot?
            url=baidu.com
            &token=YOUR_API_TOKEN

参数

以下是您可以在请求中添加的所有参数。

需要注意的是在发起 GET 请求时您务必将所有参数进行URL编码

参数 类型 默认值 描述
token string '' 您的API token
url string '' 需要截图的URL
width int 1280 浏览器宽度 单位是像素
height int 800 浏览器高度 单位是像素
full_page integer 0 如果传1 则会进行网页长截图
lazy_load integer null 如果传1 则会进行懒加载渲染
selector string null 如果传入该值 则对该选择器对应的区块截图
retina integer null 如果传1 则会进行Retina高清2K截图
fresh integer null 如果传1 则会重新进行截图 (而不是使用缓存的截图)
output string 'image' 您可以选择使用 json 作为输出格式, 或者选择 image 则会输出原生图片格式(此种方式暂时下线,请传递json获取)
delay int 500 在截屏之前需要等待多少毫秒。
device string pc 使用的设备 pc表示桌面设备 mobile表示手机设备 table表示平板设备
cookies string '' 要在浏览器中设置的cookie。如果发送多个cookie,请使用;to 分割他们。 例子: cookie1=myValue;cookie2=myOtherValue
zone string '' 区域 默认是国内线路 对国内网站进行截图推荐使用 若要对国际站点进行截图 可以传入 hk则会效果更佳

错误

如果请求的HTTP状态代码返回4xx 或者 5xx 此时返回的数据将会带有一个msg字段表示错误原因。
下面是当使用了错误token时返回的例子。

                
{
   "code": 422,
   "data": [

   ],
   "msg": "token不正确"
}
                
            

代码示例

下面的代码示例展示了如何将网站截图保存到文件中。
您的个人API令牌已经插入到示例中。

var fs = require('fs')
var request = require('request');

// 参数
var token =  'YOUR_API_TOKEN';
var url = encodeURIComponent('https://www.baidu.com');
var width = 1280;
var height = 800;
var full_page = 1;

// 构造URL
var query = "https://www.screenshotmaster.com/api/v1/screenshot";
query += `?token=${token}&url=${url}&width=${width}&height=${height}&full_page=${full_page}`;

// 调用API
request.get({url: query, encoding: 'binary'}, (err, response, body) => {
    fs.writeFile("screenshot.png", body, 'binary', err => {
        if (err) {
            console.log(err);
        } else {
            console.log("The file was saved!");
        }
    });
});
//参数
$token = 'YOUR_API_TOKEN';
$url = urlencode('https://www.baidu.com');
$width = 1200;
$height = 800;
$full_page = 1;

// 构造URL
$query = "https://www.screenshotmaster.com/api/v1/screenshot";
$query .= "?token=$token&url=$url&width=$width&height=$height&full_page=$full_page";

// 调用API
$image = file_get_contents($query);

// 保存截图
file_put_contents('./screenshot.png', $image);
package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    url2 "net/url"
    "os"
)

func main() {
    // 参数
    token := "YOUR_API_TOKEN"
    url := url2.QueryEscape("https://www.baidu.com")
    width := 1280
    height := 800
    full_page := 1

    // 构造URL
    query := "https://www.screenshotmaster.com/api/v1/screenshot"
    query += fmt.Sprintf("?token=%s&url=%s&width=%d&height=%d&full_page=%s",
        token, url, width, height, full_page)

    // 调用API
    resp, err := http.Get(query)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    // 检查是否调用成功
    if resp.StatusCode != 200 {
        errorBody, _ := ioutil.ReadAll(resp.Body)
        panic(fmt.Errorf("error while calling api %s", errorBody))
    }

    // 保存截图
    file, err := os.Create("./screenshot.png")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    _, err = io.Copy(file, resp.Body)
    if err != nil {
        panic(err)
    }
}

package main;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;

public class Main {

    public static void main(String[] args) {
        try {
            // 参数
            String token = "YOUR_API_TOKEN";
            String url = URLEncoder.encode("https://www.baidu.com");
            int width = 1280;
            int height = 800;
            int full_page = 1;

            // 构造URL
            String query = "https://www.screenshotmaster.com/api/v1/screenshot";
            query += String.format("?token=%s&url=%s&width=%d&height=%d&full_page=%s",
                    token, url, width, height, full_page);
            URL apiUrl = new URL(query);

            // 调用API并将结果保存进screenshot.png
            InputStream inputStream = apiUrl.openStream();
            OutputStream outputStream = new FileOutputStream("./screenshot.png");
            inputStream.transferTo(outputStream);

            inputStream.close();
            outputStream.close();
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
import urllib.parse
import urllib.request
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

# 参数
token = "YOUR_API_TOKEN"
url = urllib.parse.quote_plus("https://www.baidu.com")
width = 1280
height = 800
full_page = 1

# 构造URL
query = "https://www.screenshotmaster.com/api/v1/screenshot"
query += "?token=%s&url=%s&width=%d&height=%d&full_page=%s" % (token, url, width, height, full_page)

# 调用API
urllib.request.urlretrieve(query, "./screenshot.png")