分享基于phantomjs的web截图
来源:互联网 发布:ubuntu 16.04 安装后 编辑:程序博客网 时间:2024/06/04 01:25
main.go
<pre name="code" class="plain">package mainimport ( "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 stringfunc init() { _, filename, _, _ := runtime.Caller(1) curDir = path.Dir(filename)}var rootCfg Configfunc 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.Mutexconst shortFormat = "20060102"//collectionconst ( ScreenCollection = "Screen")//id_generatorconst ( 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 directoryif (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
- 分享基于phantomjs的web截图
- phantomjs截图的实践
- 基于phantomJs的Java后台网页截图技术
- 使用phantomjs获得web页面截图
- phantomjs对页面的某个div截图
- phantomjs+python截图乱码及其截图分辨率的解决办法
- CasperJS,基于PhantomJS的工具包
- php phantomjs 网页截图
- Phantomjs截图乱码
- phantomjs设置截图大小
- PhantomJS网页截图
- phantomjs设置截图大小
- phantomjs 抓取、截图中文网站乱码的问题的解决
- phantomjs 抓取、截图中文网站乱码的问题的解决
- phantomjs 抓取、截图中文网站乱码的问题的解决
- phantomjs页面截图和页面自动化的应用
- phantomJS区域截图及保存文本的简单例子
- [译]CasperJS,基于PhantomJS的工具包
- Stop Adobe Update Manager from launching
- .NET处理CSV文件 插件CSVReader
- 深入浅出Linux设备驱动编程--Linux内核模块
- 推荐14款非常有用的 CSS 网格系统生成工具
- 兔子--SDK,ADT,AVD,IDE,ADB
- 分享基于phantomjs的web截图
- 网络采集软件核心技术剖析系列(2)---如何使用C#语言获得任意站点博文的正文及标题
- Heritrix在Eclipse下的配置方法
- CSS学习笔记一
- 用ScriptableObject打包数据(以csv文件为例)
- 网页背景图固定不动,不跟随滚动条滚动
- cmd命令卸载ie9、10、11
- Sqoop导入关系数据库到Hive
- 从两道经典试题谈C/C++中联合体(union)的使用