js 爬虫抓取网页数据(Web应用功能示例SlimerJS示例PhantomJS示例示例)

优采云 发布时间: 2021-10-06 00:25

  js 爬虫抓取网页数据(Web应用功能示例SlimerJS示例PhantomJS示例示例)

  函数(您可以使用JavaScript模拟浏览器上的几乎任何操作)

  用法(与phantom JS无差异):

  页面某个区域的屏幕截图,slimerjs和phantom JS示例

  Slimerjs示例

  var webpage = require('webpage').create();

webpage.open('http://www.meizu.com') // 打开一个网页

.then(function() { // 页面加载完成后执行

//保存页面截屏

webpage.viewportSize = {

width: 650,

height: 320

};

webpage.render('page.png', {

onlyViewport: true

});

//再打开一个网页

return webpage.open('http://bbs.meizu.com');

})

.then(function() {

// 点击某个位置

webpage.sendEvent("click", 5, 5, 'left', 0);

slimer.exit(); //退出

});

  幻影JS示例

  var webpage = require('webpage').create();

webpage.open('http://www.meizu.com', function (status) {

//打开一个页面

}).then(function(){

//保存页面截屏

webpage.viewportSize = {

width: 650,

height: 320

};

webpage.render('page.png', {

onlyViewport: true

});

//再打开一个网页

return webpage.open('http://bbs.meizu.com');

}).then(function(){

webpage.sendEvent("click", 5, 5, 'left', 0);

phantom.exit();

});

  3、CasperJS

  源代码:

  官方网站:

  帮助文档:

  当前版本1.1.3

  Casperjs是一个开源的导航脚本和测试工具。它是用基于phantom JS的javascript编写的,用于测试web应用程序功能。Phantom JS是服务器端JavaScript API的WebKit。它支持各种web标准:DOM处理、CSS选择器、JSON、画布和SVG

  Casperjs根据start()、then*()、wait*()、open()进程向下导航(注意,如果出现语法错误,例如神马没有分号,则可能有一张卡在运行时没有任何提示)

  run()方法触发该进程。run()方法可以在导航完成时为回调指定oncomplete()方法

  退出()/die()退出

  这个。Getpagecontent()可以查看呈现的页面内容

  它为web应用程序测试提供了一组方法组件。这些组件基于phantom JS或slimerjs提供的JavaScript API实现web应用程序的功能执行。Casperjs简化了完整导航场景的流程定义,并为完成常见任务提供了实用的高级功能、方法和语法。例如:

  用法:

  Casperjs前端自动化测试脚本示例:

  var utils = require('utils');

var webpage = require('casper').create({

//verbose: true,

logLevel: 'debug',

viewportSize: {

width: 1024,

height: 768

},

pageSettings: {

loadImages: true,

loadPlugins: true,

XSSAuditingEnabled: true

}

});

//打开页面

webpage.start()

.thenOpen('http://www.meizu.com', function openMeizu(res) {

this.echo('打印页面信息');

res.body = '';//不打印body信息

utils.dump(res);

//点击登录按钮

if (this.exists("#_unlogin")) {

this.echo('点击登录按钮');

this.click("#_unlogin a:nth-child(1)");

this.wait(3000, function wait3s_1() {

if (this.exists("form#mainForm")) {

this.echo("需要登陆,填充账号信息。。。");

//填充表单账号

this.fill('form#mainForm', {

'account': 'lzwy0820@flyme.cn',

'password': '********'

}, true);

this.capture('meizu_login_page.png');

this.wait(3000, function wait3s_2() {

//登录按钮存在,点击

if (this.exists("#login")) {

this.echo('提交登录');

this.click("#login");

}

});

}

});

}

})

.then(function capture() {

if (this.exists('#mzCustName')) {

this.echo('登录成功!开始截图存储..');

} else {

this.echo('登录失败!请查看截图文件')

}

//截图

this.capture('meizu.png');

this.captureSelector('meizu_header.png', 'div.meizu-header');

})

.then(function exit() {

this.echo('执行完成,退出');

this.exit();

})

.run();

  casperjs捕获的JavaScript呈现的网页示例:

  下面的代码将抓取网易云音乐页面,运行JavaScript,然后将最终的HTML保存在文本中

  // http://casperjs.org/

var casper = require('casper').create({

pageSettings: {

loadImages: false, // 不加载图片,减少请求

}

});

var fs = require('fs');

casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36');

casper.start();

casper.viewport(1024, 768);

casper.then(function() {

this.open('http://music.163.com/#/m/discover/artist/').then(function(){

this.page.switchToChildFrame(0); // 页面包含 iFrame

fs.write("1.html", this.getPageContent(), 'w'); // 保存 iFrame 里的内容

this.page.switchToParentFrame();

fs.write("2.html", this.getPageContent(), 'w');

});

});

casper.run();

  Casperjs循环调用示例:

  解决方案是递归调用。当casperjsobj调用run函数时,它可以传入一个要在最后执行的函数。在这个函数中,我们可以添加[our loop body]和[recursive run call]

  function refresh()

{

this.wait(10000,

function() {

this.click('a[title="简历刷新"]');

this.log('refreshed my resume');

}

);

this.run(refresh);

}

casper.run(refresh);

  日志中会显示信息消息“不安全的JavaScript尝试使用URL访问帧”。启动casperjs时,添加参数-Web Security=false(允许跨域XHR)(请参阅)

  要解决根本原因,请参阅

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线