Automatically browse pages with Casperjs (GO)

Source: Internet
Author: User

Content transfer from http://blog.csdn.net/kiwi_coder/article/details/36248353

Casperjs is a PHANTOMJS-based tool that makes it easier to navigation than PHANTOMJS.

One of the simplest CASPERJS codes

Create a file baidu.js to simulate our visit to Baidu page

var casper = require (' Casper '). Create ();

Casper.start (' http://www.baidu.com/', function () {
This.echo (This.gettitle ());
});

Casper.run ();

Run:

Casperjs Baidu.js

Get output:

"Baidu a bit, you know"

A second simple CASPERJS code

The execution script for Casperjs is a concatenation of one step. Start represents the first step, then the subsequent step is then represented by then, followed by:

var casper = require (' Casper '). Create ();

Casper. Start (' http://www.baidu.com/', function () {
This.echo (This.gettitle ());
});

Casper. Then (function () {
This.capture (' baidu-homepage.png ');
})

Casper.run ();

When we're done, we'll get a title on the console, and we'll get the picture captured in then Baidu-homepage.jpg

A third simple CASPERJS code

We're trying to get Casperjs to complete the search function:

var casper = require (' Casper '). Create ();

Casper.start (' http://www.baidu.com/', function () {
This.echo (This.gettitle ());
});

Casper.then (function () {
This.capture (' baidu-homepage.png ');
});

Casper.then (function () {
This.fill (' form[action= '/S "] ', {wd: ' ThoughtWorks '}, True);//fill in a form to search
});

Casper.then (function () {
This.capture (' thoughtworks-search-results.png ');
});

Casper.run ();

Get the Thoughtworks-search-results.png:

How to introduce jquery

In some cases, jquery is convenient.

var casper = require (' Casper '). Create ({
Clientscripts: ["Jquery.js"]
});

Casper.start (' http://www.baidu.com/', function () {
This.echo (This.gettitle ());
});

Casper.then (function () {
This.fill (' form[action= '/S "] ', {wd: ' ThoughtWorks '}, True);
});

Casper.then (function () {
Search_result_titles = this.evaluate (gettitles)
This.echo (Search_result_titles.join (' \ n '))
});

function GetTitles () {
var titles = $.map ($ ("h3.t a"), function (link) {
return $ (link). Text ()
});
Return titles
}

Casper.run ();

return Result:

Thoughtworks_ Baidu Encyclopedia
Chengdu thoughtworks-Recruitment Specialist--Location: Chengdu Job Information | ThoughtWorks recruitment ...
Agile Development and Experiential design | ThoughtWorks
ThoughtWorks Basic situation and treatment "Coward Savior _ Workplace Ancient Boxing Law"
An encounter with ThoughtWorks (i)-Redhat-iteye technology website
ThoughtWorks written Zz_thoughtworks Recruitment experience
About Us | ThoughtWorks
ThoughtWorks ranked first in the interview of the most difficult technology companies _ Baidu Library
Transparent album-thoughtworks XI ' an office
Stevok Software Technology (XI ' an) Co., Ltd. ThoughtWorks software ...

Places to be aware of:

1) Create Casper, we inject jquery, this jquery must be stored locally, and HTTP access is not valid.

2) this.evaluate (gettitles) can be understood, we in Casperjs, the GetTitles this method is injected into the visited page, in the visited page to execute this method and ask its return value.

3) access page log gets: 2) The gettitles is actually executed in the visited page, if we add a section of Console.log code in GetTitles, how can we get the console information of the page being visited?

Casper.then (function () {
This.page.onConsoleMessage = function (e) {
Console.log (e);
}

Search_result_titles = This.evaluate (gettitles)
This.echo (Search_result_titles.join (' \ n '))
})

This allows you to listen for the Console.log event of the page being accessed, rather than exporting to Casperjs

Reference: Http://docs.casperjs.org/en/latest/faq.html#can-i-use-jquery-with-casperjs

Automatically browse pages with Casperjs (GO)

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.