Skip to content

Instantly share code, notes, and snippets.

@liuxd
Last active September 7, 2017 01:29
Show Gist options
  • Save liuxd/c6e175e2663adaadecfe to your computer and use it in GitHub Desktop.
Save liuxd/c6e175e2663adaadecfe to your computer and use it in GitHub Desktop.
[phantomjs-sample.js] A sample for phantomjs,fetch index.baidu.com's data.
#!/usr/bin/env phantomjs
var fs = require('fs');
var page = require('webpage').create();
// Add cookie.
var cookie = fs.read('baidu.cookie');
var cookie_lines = cookie.split("\n");
for (i in cookie_lines) {
var fields = cookie_lines[i].split(' ');
phantom.addCookie({
'name': fields[0],
'value': fields[1],
'domain': fields[2],
'path': fields[3],
});
}
// Fetch page.
var url = 'http://index.baidu.com/?tpl=crowd&word=360';
page.open(url, function(status) {
page.injectJs('jquery-2.1.4.min.js');
if(status === "success") {
var ret = page.evaluate(function(){
return $('#grp_social_l').html().replace("\n", '');
});
console.log(ret);
}
phantom.exit();
});
// end of this file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment