Skip to content

Instantly share code, notes, and snippets.

@tka
Last active June 3, 2016 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tka/f71b2e5ccc87fd5a5ef26ac6c12df3b6 to your computer and use it in GitHub Desktop.
Save tka/f71b2e5ccc87fd5a5ef26ac6c12df3b6 to your computer and use it in GitHub Desktop.
phatomjs 2.1 網頁抓圖, 可用 css selector 或是 xpath 抓出區塊
"use strict";
var page = require('webpage').create(),
system = require('system'),
address, output, size, pageWidth, pageHeight, targetSelector;
if (system.args.length < 3 || system.args.length > 4) {
console.log('Usage: screenshot.js URL filename [css selector|xpath:path]');
console.log('example: phatomjs screenshot.js https://google.com image.png img');
console.log('example: phatomjs screenshot.js https://google.com image.png xpath://img');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
pageWidth = parseInt('1024px', 10);
pageHeight = parseInt('768px', 10); // it's as good an assumption as any
page.viewportSize = { width: pageWidth, height: pageHeight };
if (system.args.length > 3) {
targetSelector = system.args[3];
}
console.log('open: ' + address);
console.log('selector: ' + targetSelector);
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit(1);
} else {
var rect = page.evaluate(function (selector) {
document.body.bgColor = "white";
if (selector === undefined) {
return null;
}
console.log(selector)
var elem
if(selector.indexOf("xpath:") == 0){
elem = document.evaluate(selector.substr(6), document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
} else {
elem = document.querySelector(selector);
}
return (elem !== null) ? elem.getBoundingClientRect() : null;
}, targetSelector)
if (rect !== null) {
page.clipRect=rect;
}
// Set a timeout to give the page a chance to render
setTimeout(function () {
page.render(output);
phantom.exit(1);
}, 250);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment