废话不多说,这里以截取grafana监控试图为例。
- 主python代码
#!/usr/bin/env python
#coding: utf-8
import os
import sys
import time
import commands
import logging
def ret_html_node():
commands.getoutput("/usr/bin/phantomjs --ignore-ssl-errors=true /ext/data/env/monitor/mail/node_grafana.js")
html_table_node = '''<table border="0" cellpadding="20" cellspacing="0" width="100%">
<tr>
<td valign="top" class="bodyContent">
<div mc:edit="std_content00">
<strong>xxx视图</strong>
</tr>
<tr>
<td valign="top" style="padding-top:0; padding-bottom:0;">
<img src="cid:image_2" alt="digglife">
</td>
</tr>
</table>'''
return html_table_node
if __name__ == "__main__":
ret_html_node()
- 加载网页代码
var loginURL = 'https://aa.bb.com/grafana/login';
var url='https://aa.bb.com/grafana/d/R0gfyLRWk/cc?orgId=1&kiosk=tv&from=now-1d%2Fd&to=now-1d%2Fd';
const page = require('webpage').create();
const data = '{"user": "username", "password": "pwd123", "email": ""}';
const headers = {
"Content-Type": "application/json"
};
page.open(loginURL, 'post', data, headers, function (status) {
if (status === 'success') {
page.open(url, function(status) {
// 打开要截图的页面成功
if (status === 'success') {
console.log('打开要截图的页面成功');
page.viewportSize = { width: 1280, height: 1900 };
setTimeout(function () {
page.render('/ext/data/env/monitor/mail/node_data.png');
phantom.exit();
}, 10000);
} else {
console.log('打开要截图的页面时发生错误!');
phantom.exit();
}
});
} else {
console.log("打开登录页面错误!");
phantom.exit();
}
});
- 执行以下代码生成图片
python node_grafana.py