获取网页指定url
var links=[] var casper=require('casper').create({verbose:true,logLevel:"debug"}); function getlinks(){ var links=document.querySelectorAll('div ol li a'); return Array.prototype.map.call(links,function(e){ return e.getAttribute('href'); }); } casper.start("http://bbs.wdzj.com",function(){ links=this.evaluate(getlinks); /* this.echo(links) */ }) casper.run(function(){ this.echo(links.length+' links found:'); this.echo(''+links.join('\n')).exit(); })
运行
$ casperjs links.js
Warning PhantomJS v2.0 not yet released. There will not be any official support for any bugs until stable version is released! [info] [phantom] Starting... [info] [phantom] Running suite: 2 steps [debug] [phantom] opening url: http://bbs.wdzj.com/, HTTP GET [debug] [phantom] Navigation requested: url=http://bbs.wdzj.com/, type=Other, willNavigate=true, isMainFrame=true [debug] [phantom] url changed to "http://bbs.wdzj.com/" [debug] [phantom] Successfully injected Casper client-side utilities [info] [phantom] Step anonymous 2/2 http://bbs.wdzj.com/ (HTTP 200) [info] [phantom] Step anonymous 2/2: done in 1626ms.
[info] [phantom] Done 2 steps in 1641ms
166 links found:
http://bbs.wdzj.com/thread-811591-1-1.html
http://bbs.wdzj.com/thread-811588-1-1.html
http://bbs.wdzj.com/thread-109128-1-1.html
http://bbs.wdzj.com/thread-811537-1-1.html
http://bbs.wdzj.com/thread-811533-1-1.html
http://bbs.wdzj.com/thread-811519-1-1.html
http://bbs.wdzj.com/thread-811435-1-1.html
http://bbs.wdzj.com/thread-811357-1-1.html
http://bbs.wdzj.com/thread-811305-1-1.html
http://bbs.wdzj.com/thread-808332-1-1.html
http://bbs.wdzj.com/thread-808202-1-1.html
http://bbs.wdzj.com/thread-810970-1-1.html
http://bbs.wdzj.com/thread-811582-1-1.html
http://bbs.wdzj.com/thread-811575-1-1.html
http://bbs.wdzj.com/thread-811561-1-1.html
http://bbs.wdzj.com/thread-811547-1-1.html
http://bbs.wdzj.com/thread-811525-1-1.html
http://bbs.wdzj.com/thread-811090-1-1.html
http://bbs.wdzj.com/thread-811504-1-1.html
http://bbs.wdzj.com/thread-811478-1-1.html
http://bbs.wdzj.com/thread-811469-1-1.html
http://bbs.wdzj.com/thread-811443-1-1.html
http://bbs.wdzj.com/thread-811410-1-1.html
http://bbs.wdzj.com/thread-811469-1-1.html
http://bbs.wdzj.com/thread-811525-1-1.html
......................此处省略
{测试窝原创文章,签约作者:江永道}
作者简介:江永道,09届毕业,6年软件测试行业经验