fixes #100 - googlepagination.coffee broken
Showing
1 changed file
with
20 additions
and
26 deletions
1 | """ Capture multiple pages of google search results | 1 | # Capture multiple pages of google search results |
2 | # | ||
3 | # usage: casperjs googlepagination.coffee my search terms | ||
4 | # | ||
5 | # (all arguments will be used as the query) | ||
2 | 6 | ||
3 | usage: casperjs googlepagination.coffee my search terms | ||
4 | |||
5 | (all arguments will be used as the query) | ||
6 | """ | ||
7 | casper = require('casper').create() | 7 | casper = require('casper').create() |
8 | currentPage = 1 | ||
8 | 9 | ||
9 | if casper.cli.args.length == 0 | 10 | if casper.cli.args.length == 0 |
10 | casper.echo "usage: $ casperjs my search terms" | 11 | casper.echo "usage: $ casperjs my search terms" |
11 | casper.exit() | 12 | casper.exit() |
12 | 13 | ||
13 | casper.start 'http://google.com', -> | 14 | processPage = -> |
14 | @fill 'form[action="/search"]', q: casper.cli.args.join(' '), true | 15 | casper.echo "capturing page #{currentPage}" |
15 | @click 'input[value="Google Search"]' | 16 | casper.capture "google-results-p#{ currentPage }.png" |
16 | |||
17 | casper.then -> | ||
18 | # google's being all ajaxy, wait for results to load... | ||
19 | @waitForSelector 'table#nav', -> | ||
20 | processPage = (cspr) -> | ||
21 | currentPage = Number cspr.evaluate(-> document.querySelector('table#nav td.cur').innerText), 10 | ||
22 | currentPage = 1 if currentPage == 0 | ||
23 | cspr.capture "google-results-p#{ currentPage }.png" | ||
24 | 17 | ||
25 | # don't go too far down the rabbit hole | 18 | # don't go too far down the rabbit hole |
26 | return if currentPage >= 5 | 19 | return if currentPage >= 5 |
27 | 20 | ||
28 | cspr.evaluate -> | 21 | if casper.exists "#pnnext" |
29 | if nextLink = document.querySelector('table#nav td.cur').nextElementSibling?.querySelector('a') | 22 | currentPage++ |
30 | nextLink.setAttribute "id", "next-page-of-results" | 23 | casper.echo "requesting next page: #{currentPage}" |
31 | 24 | casper.thenClick("#pnnext").then(processPage) | |
32 | nextPage = "a#next-page-of-results" | ||
33 | if cspr.exists nextPage | ||
34 | cspr.echo "requesting next page..." | ||
35 | cspr.thenClick(nextPage).then(processPage) | ||
36 | else | 25 | else |
37 | cspr.echo "that's all, folks." | 26 | casper.echo "that's all, folks." |
27 | |||
28 | casper.start 'http://google.fr/', -> | ||
29 | @fill 'form[action="/search"]', q: casper.cli.args.join(' '), true | ||
38 | 30 | ||
39 | processPage(casper) | 31 | casper.then -> |
32 | # google's being all ajaxy, wait for results to load... | ||
33 | @waitForSelector 'table#nav', => processPage(casper) | ||
40 | 34 | ||
41 | casper.run() | 35 | casper.run() | ... | ... |
-
Please register or sign in to post a comment