LoginSignup
9
9

More than 5 years have passed since last update.

CasperJSでファイルダウンロード(途中まで)

Posted at

タイトルままです。
ファイルダウンロード時にリソースロード失敗でファイルダウンロードできません。

casper_download_ga.coffee
#!/usr/bin/env casperjs
# グーグルアナリティクスへログインし日別PV数のTSVエクスポートデータを取得する

casper = require('casper').create
    verbose: true
    loglevel: 'debug'
    pageSettings:
        "webSecurityEnabled": false
        "ignoreSslErrors": true
        "userAgent": 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120427 Firefox/15.0a1'

x = require('casper').selectXPath

if casper.cli.args.length isnt 2
    casper.log 'Missing required argument. / USER PASS', 'error'
    casper.exit()

user_name = casper.cli.args[0]
user_pass = casper.cli.args[1]

accessLog = ()->
    casper.viewport 1024, 768
    casper.echo "================================"
    casper.echo casper.evaluate -> document.URL
    casper.echo "================================"

casper.start 'https://accounts.google.com/ServiceLogin?service=analytics&passive=true&nui=1&hl=ja&continue=https%3A%2F%2Fwww.google.com%2Fanalytics%2Fweb%2F%3Fhl%3Dja&followup=https%3A%2F%2Fwww.google.com%2Fanalytics%2Fweb%2F%3Fhl%3Dja', ->
    accessLog()

casper.then ->
    @echo "LOGIN"
    accessLog()
    @fill 'form#gaia_loginform',
        "Email": user_name
        "Passwd": user_pass
        , true

casper.then ->
    @echo "SELECT FIRST VIEW LINK"
    accessLog()
    @click 'div._GAJA._GAbV._GANV a'

casper.then ->
    @echo "DATE SETTINGS"
    accessLog()
    from_date = '20121001'
    to_date = '20130430'
    overview_url = @getCurrentUrl()
    date_url = '%3F_u.date00%3D' + from_date + '%26_u.date01%3D' + to_date + '/'
    @echo overview_url + date_url
    @open overview_url + date_url

casper.then ->
    @echo "SELECT DETAIL DATA LINK"
    accessLog()
    casper.waitForSelector 'div._GAg._GAoGb.ACTION-deepLink.TARGET-none', -> 
        @click 'div._GAg._GAoGb.ACTION-deepLink.TARGET-none'

casper.then ->
    @echo "SELECT CONVERSION"
    @wait 1500, ->
        accessLog()
        @click x('//*[@id="ID-explorer-graphOptions"]/div[1]/div[2]/div[4]/div/div[1]')
        @wait 10, ->
            @sendKeys 'input.ID-searchBox', '目標 1 のコンバージョン'
            @click 'div.ID-compareConceptSelector._GAn1 input[type="checkbox"][class="ACTION-toggle TARGET-view"]'
            @wait 10, ->
                @click 'div.ID-compareConceptSelector._GAn1 div[class*="goalConversionRate1"]'

casper.then ->
    @echo "DOWNLOAD TSV"
    @wait 2000, ->
        accessLog()
        @click 'span.ID-exportControlButton._GAjc.ACTION-exportMenu.TARGET-'
        @click 'li.ACTION-export.TARGET-TSV' # ダウンロード開始のハズ
        url = 'https://www.google.com/analytics/web/exportReport?hl=ja&authuser=0&ef=TSV'
        file = "stats.tsv"
        try
            # ...
            @echo "Attempting to download file " + file
            fs = require 'fs'
            casper.download(url, fs.workingDirectory + '/' + file)
        catch e
            # ...
            @echo e

casper.then ->
    @echo "TEST COMPLETE"
    @wait 1000, ->
        accessLog()
        @capture "complete.png"
        @exit()

casper.run()

これを実行すると、以下のキャプチャは取れます。
complete.png

実際に取得したstats.tsvファイルはこんな感じになってます。

stats.tsv
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>エラーが発生しました</title></head><body><h2>エラーが発生しました</h2>サービスが一時的にご利用いただけません。申し訳ございませんが、数分後に再度お試しください。  </body> 
</html>

デバッグログには以下のようなメッセージが出力されており、リソースのロードに失敗しているらしいことが伺えますね。
[warning] [phantom] Loading resource failed with status=fail (HTTP 200): https://www.google.com/analytics/web/exportReport?hl=ja&authuser=0&ef=TSV

9
9
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9