googlepagination.coffee
949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Capture multiple pages of google search results
#
# usage: casperjs googlepagination.coffee my search terms
#
# (all arguments will be used as the query)
casper = require('casper').create()
currentPage = 1
if casper.cli.args.length == 0
casper
.echo "usage: $ casperjs my search terms"
.exit(1)
processPage = ->
@echo "capturing page #{currentPage}"
@capture "google-results-p#{currentPage}.png"
# don't go too far down the rabbit hole
return if currentPage >= 5
if @exists "#pnnext"
currentPage++
@echo "requesting next page: #{currentPage}"
#@thenClick("#pnnext").then(processPage)
url = @getCurrentUrl()
@thenClick("#pnnext").then ->
check = -> url != @getCurrentUrl()
@waitFor check, processPage
else
@echo "that's all, folks."
casper.start 'http://google.fr/', ->
@fill 'form[action="/search"]', q: casper.cli.args.join(' '), true
casper.then processPage
casper.run()