Small text notes-Phantomjs

Source: Internet
Author: User

# Small Text notes-phantomjs2017-05-13## first section: Install "txt Windows installation: Download unzip file My Computer--Properties--Advanced system settings Click Environment variable in the System Variables window, locate the PATH variable Volume, select and then select Edit New, add new environment variable (phantomjs.exe) where the path is in cmd, run Phantomjs mac installation: Download unzip file in User directory view Ls-a view all files open. bash_profile File added to export path= $PATH:/users/suoyong/phantomjs-2.1.1-macosx/bin Enter PHANTOMJS run "phantomjs-v" in terminal to view the version. ' Phantomjs a.js ' run ' a.js ' file. ' Phantomjs ' into the program, ' Console.log (1) ', run the JS code. ' Phantom.exit () ' Exits. # # # II: Core module ' txt Web page module file module System Module sub-process Module Network Service Module website phantomjs.org/api/' use ' require ( ' Module name ') ' Reference module.  A.js ' js var sys = require (' system ');  Console.log (Sys.args); Phantom.exit (); Run, A.js is the first parameter, HI is the second parameter. "Bat Phantomjs a.js hi A.js,hi" # #: Webpage object (i) "TXT create webpage object: Create () Open URL: Page.open Action page in Evaluate method (Dom,json,canvas, etc.) to save the current page as a picture: Page.render (). Create () Creates a webpage object instance. Open () Opens the URL. Evaluate () in order to manipulate the inside of the Web interface. Capacity.  "' Open Baidu homepage title ' ' js var webpage = require (' webpage '); var page = Webpage.create(); Page.open (' https://www.baidu.com ', function () {///Open after doing things var title = Page.evaluate (function () {//Operation page return Docum    Ent.title});    Console.log (title); Phantom.exit (1);  1 Success 0 Failed} "open object" JS var webpage = require (' webpage ');  var page = Webpage.create (); Page.open (' https://www.baidu.com ', function () {///Open after doing things var title = Page.evaluate (function () {//Operation page return Docum    Ent.getelementbyid (' LG ');    }); Console.log (title)//[Object Object], he other is an object//Console.dir (title)//[Object Object], object Console.log (json.str Ingify (title)); So we need to convert phantom.exit (1); 1 Success 0 Failure}) ' # # III: Webpage object (ii) the DOM information cannot be displayed directly in evaluate, because evaluate is running in the sandbox in order not to affect the normal operation of the other pages, he has no cons Ole.log.  ' js var webpage = require (' webpage ');  var page = Webpage.create (); Page.open (' https://www.baidu.com ', function () {page.evaluate (function () {console.log (' LG ') ));    There is no console.log () output.    });  Phantom.exit (1); }) ' ResolveMethod: Add a callback function for the webpage object.  ' js var webpage = require (' webpage ');  var page = Webpage.create ();  Page.onconsolemessage = function (msg) {Console.log (msg);    } page.open (' http://www.intalesson.com/', function () {page.evaluate (function () {console.log (document.title);    });  Phantom.exit (1); } # # # # # # # # # # # # # # # # # # # #  ' js var webpage = require (' webpage ');  var page = Webpage.create ();  Page.onconsolemessage = function (msg) {Console.log (msg);    } page.open (' http://www.intalesson.com/', function () {var title = Page.evaluate (function (ARG) {Console.log (ARG)};    }, ' Hi ');  Phantom.exit (1); }) ' # # # Set User agent ' Page.settings.userAgent = ' users agent to set ' # # # Fifth: Submit Form-' page.onloadfinished = function () {} ' when the page is finished loading Function. -' dom.submit () ' Submit. -' Simulate Click event ' submission. Demo login Zhaopin and save.  ' js var webpage = require (' webpage ');  var page = Webpage.create (); Page.open (' http://www.zhaopin.com/', function () {page.evaluate (function () {var user = document.getElementById (' Logi Nname ');      var pass = document.getElementById (' password ');      User.value = ' user name ';      Pass.value = ' password ';      var submit = Document.queryselector ('. logbtn button '); var evt = document.createevent (' mouseevents '); Create a mouse event evt.initmouseevent (' click '); Initializes a mouse click event submit.dispatchevent (EVT); Use event, submit form}) Page.onloadfinished = function (status) {if (status = = ' success ') {//Check whether the page has finished loading page.rend ER (' 1.png ');      Save the page picture Phantom.exit (1); }}) ' # # # Sixth: Operation Cookie-cookies View-Addcookie settings Be sure to set the domain name value three when setting a cookie.  ' js Phantom.addcookie ({' domain ': '. baidu.com ', ' name ': ' XW ', ' value ': ' 1 '});  Console.log (Json.stringify (phantom.cookies)); Phantom.exit (1); "' # # # Seventh: Casperjscasperjs extended from PHANTOMJS to make it easier to manipulate page elements. PHANTOMJS is not a Nodejs module, but Casperjs can be installed with NPM. -' casper.start () = Page.open () '-' echo () = Console.log () ' Phantomjs like JS, Casperjs like JQ. -Install Casperjs ' npm install-g casperjs ' # # Eighth: Stepping scripting language # # Small text note garbled problem: Http://blog.csdn.net/kaosIni/article/details/47252457-method One: Add ' phantom.outputencoding= ' GBK "' in JS file to solve garbled characters. -Method Two: ' Phantomjs--output-encoding=gbk a.js '

Original link: http://www.cnblogs.com/daysme/p/6850956.html

Small text notes-Phantomjs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.