分享基于phantomjs的web截图

main.go
<pre name="code" class="plain">package main

import (
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"
    "os/exec"
    "path"
    "runtime"
    "strconv"
    "strings"
    "sync"
    "time"
)


type App struct {
    Domain string
    Post   int
    ScreenJS   string
    ScreenPath string
    Static     string
}



type Config struct {
    Resource Resource
    RootDir  string //项目根目录
    App      *App
    Log      *log.Config
}

var curDir string

func init() {
    _, filename, _, _ := runtime.Caller(1)
    curDir = path.Dir(filename)
}

var rootCfg Config

func conf() Config {
    if rootCfg.RootDir == "" {
        rootCfg.RootDir = path.Join(curDir, "./")
        var f = rootCfg.RootDir + "/dsp_screen.json"
        data, err := ioutil.ReadFile(f)
        if err != nil {
            panic(err)
        }
        err = json.Unmarshal(data, &rootCfg)
        if err != nil {
            panic(err)
        }
    }
    return rootCfg
}

var dbs = make(map[string]*mongo.Mdb)
var mutex sync.Mutex


const shortFormat = "20060102"

//collection
const (
    ScreenCollection = "Screen"
)

//id_generator
const (
    idGenerator = "id_generator"
)

type Screen struct {
    Id         int64  `bson:"_id"`
    Url        string `bson:"Url"`
    UserAgent  string `bson:"UserAgent"`
    Path       string `bson:"Path"`
    CreateTime int64  `bson:"CreateTime"`
}

// func (this *Screen) Init() {
//     id := nextId(ScreenCollection)
//     this.Id = id
//     this.CreateTime = time.Now().Unix()
// }

// func (this *Screen) IsInit() bool {
//     return this.Id != 0
// }

// func (this *Screen) Save() (err error) {
//     return appMdb().Insert(ScreenCollection, this)
// }

func screen(rw http.ResponseWriter, req *http.Request) {
    defer req.Body.Close()
    data, err := ioutil.ReadAll(req.Body)
    if err != nil {
        WriteError(rw, err.Error())
        return
    }

    body := make(map[string]string)
    err = json.Unmarshal(data, &body)
    if err != nil {
        WriteError(rw, err.Error())
        return
    }

    screen := new(Screen)
    if url, ok := body["Url"]; ok {
        screen.Url = url
    }

    if data, ok := body["Data"]; ok {
        screen.UserAgent = fmt.Sprintf("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/38.0.2125.111 Chrome/38.0.2125.111 Safari/537.36?%s",
            data)
    }

    t := time.Now()
    screen.Path = fmt.Sprintf("%s/%d.png", t.Format(shortFormat), t.UnixNano())
    path := fmt.Sprintf("%s/%s", conf().App.ScreenPath, screen.Path)
    ph := exec.Command("phantomjs", conf().RootDir+"/"+conf().App.ScreenJS, screen.Url, screen.UserAgent, path)
    out, err := ph.Output()
    if err != nil {
        log.Error(err)
        WriteError(rw, err.Error())
        return
    }

    if strings.Contains(string(out), "false") {
        log.Error(string(out))
        WriteError(rw, "截图失败")
        return
    }

    // screen.Init()
    // err = screen.Save()
    // if err != nil {
    //     log.Error(err)
    //     WriteError(rw, err.Error())
    //     return
    // }

    result := make(map[string]string)
    result["url"] = conf().App.Domain + "/" + conf().App.Static + "/" + screen.Path
    WriteResult(rw, result)
}

func image(rw http.ResponseWriter, req *http.Request) {
    staticHandler := http.FileServer(http.Dir(conf().RootDir))
    staticHandler.ServeHTTP(rw, req)
    return
}

func WriteError(rw http.ResponseWriter, msg string) {
    rw.Write([]byte(`{"success":false,"message":"` + msg + `"}`))
}

func WriteResult(rw http.ResponseWriter, result interface{}) {
    data, err := json.Marshal(result)
    if err != nil {
        WriteError(rw, err.Error())
    } else {
        rw.Write([]byte(fmt.Sprintf(`{"success":false,"result":%s}`, string(data))))
    }
}

func handler(rw http.ResponseWriter, req *http.Request) {
    if strings.Contains(req.URL.String(), conf().App.Static) {
        image(rw, req)
    } else {
        screen(rw, req)
    }
}

func main() {
    err := log.InitConf(conf().Log)
    if err != nil {
        panic(err)
    }

    http.HandleFunc("/", handler)
    fmt.Println("监听端口", conf().App.Post)
    err = http.ListenAndServe(fmt.Sprintf(":%d", conf().App.Post), nil)
    if err != nil {
        log.Error(err)
        panic(err)
    }
}

 
phantomjs.js
<pre name="code" class="javascript">var page = require('webpage').create();
page.paperSize = {
	width:'5in',
	height:'7in'
};


//args
//code word directory
if (phantom.args.length < 3) {
	console.log("{'success': false, 'result': '参数不正确'}");
	phantom.exit();
};
var args = phantom.args;
var url = args[0];
var userAgent  = args[1];
var path = args[2];


page.settings.userAgent = userAgent;
page.settings.viewportSize = { width: 1920, height: 1080 };
//设置超时
// page.settings.resourceTimeout = 10000;


page.viewportSize = { width: 1920, height : 1080 };
page.open(url, function(status) {
	if (status !== 'success') {
		console.log("{'success': false, 'result': '请求出错'}");
	} else {
		page.render(path)
		console.log("{'success': true, 'result': '"+path+"'}");
	}
	phantom.exit();
});


 
配置文件
<pre name="code" class="javascript">{
    "App": {
    	"Domain":"http://localhost:9988",
    	"Post":9988,
        "ScreenJS":"phantom.js",
        "ScreenPath":"/data/screen/screen",
        "Static":"image"
    }
}


 伸手党可能有点烦,因为还是需要改一下才能运行的。主要就是一个网页截图以及展示图片。
                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值