1. Download
2. Unzip the installation package
Just unzip it directly
3. Configure environment variables
Find the advanced system settings, open it, and the following picture appears. Click on Environment Variables.
Click the edit button
respectively to add the new decompression path to the bin folder. Click on OK.
In this way, the environment variables are configured, you can use the phantomjs command directly in the command line tool .
Fourth, the code segment
Create a new JS file, such as: main.js
execute command: phantomjs main.js
1. Print the title of the specified URL
var page = require('webpage').create();
phantom.outputEncoding="gbk";
page.open("https://url.163.com/EWS", function(status) {
console.log("Status: " + status);
if ( status === "success" ) {
console.log(page.title)
} else {
console.log("Page failed to load.");
}
phantom.exit(0);
});
2. Generate screenshots of web pages
var page = require('webpage').create();
page.open('https://maomin.blog.csdn.net/', function() {
page.render('github.png');
phantom.exit();
});
3. Convert web pages to PDF
var wpage = require('webpage').create();
var url = "https://en.wikipedia.org/wiki/Main_Page";
var output = "test.pdf";
wpage.paperSize = {
width: screen.width+'px',
height: '1500px',
margin: {
'top':'50px',
'left':'50px',
'rigtht':'50px'
},
orientation:'portrait',
header: {
height: "1cm",
contents: phantom.callback(function(pageNumber, nPages) {
return "<h5>Header <b>" + pageNumber + " / " + nPages + "</b></h5>";
})
},
footer: {
height: "1cm",
contents: phantom.callback(function(pageNumber, nPages) {
return "<h5>Footer <b>" + pageNumber + " / " + nPages + "</b></h5>";
})
}
}
wpage.open(url, function (status) {
if (status !== 'success') {
console.log('Page is not opening');
phantom.exit();
} else {
wpage.render(output);
phantom.exit();
}
});