1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SpookyJSでformの値に日本語を入力する

1
Last updated at Posted at 2016-09-08

とあるページをスクレイピングするためにcasperjsを使っていたのですが、
結構癖があり消耗していたところ、spookyjs というラッパーを見つけました。

参考: SpookyJSで楽々テスト&スクレイピング

こちらよさげなのですが、日本語などの挙動が怪しく、
formの値として日本語を渡すとハングしてしまいます。

var Spooky = require('spooky');

var spooky = new Spooky({
        child: {
            command: "node_modules/casperjs/bin/casperjs.exe",
            transport: 'http'
        },
        casper: {
            logLevel: 'debug',
            verbose: true
        }
    }, function (err) {
        if (err) {
            e = new Error('Failed to initialize SpookyJS');
            e.details = err;
            throw e;
        }

        var links = [];

        spooky.userAgent('Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');

        spooky.start('https://www.google.co.jp');
        spooky.then(function () {
            var payload = {q:'東京特許許可局'};
            this.fill('form[action="/search"]',payload, true);
        });
        spooky.then(function(){
            links = this.evaluate(function(){
                var links = document.querySelectorAll('h3.r a');
                return Array.prototype.map.call(links, function(e) {
                    return e.getAttribute('href');
                });
            });
        });
        spooky.run(function(){
            this.emit('hello', links.join("\n"));
        });
    });

上記コードを実行すると固まってしまいます。
検索文字列を英数字のみにした場合は、問題なく動きます。

原因はよくわかっていないのですが、spookyjsとcasperjsとの通信部分で
パラメータをうまくわたせていない?のではないかと思います。

そこで以下のように 通信方法の指定を stdio にしてあげると、
日本語を入力してもうまく動きます。

var Spooky = require('spooky');

var spooky = new Spooky({
        child: {
            command: "node_modules/casperjs/bin/casperjs.exe",
            transport: 'stdio' // ← ここを変更する
        },
        casper: {
            logLevel: 'debug',
            verbose: true
        }
    }, function (err) {
        if (err) {
            e = new Error('Failed to initialize SpookyJS');
            e.details = err;
            throw e;
        }

        var links = [];

        spooky.userAgent('Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');

        spooky.start('https://www.google.co.jp');
        spooky.then(function () {
            var payload = {q:'東京特許許可局'};
            this.fill('form[action="/search"]',payload, true);
        });
        spooky.then(function(){
            links = this.evaluate(function(){
                var links = document.querySelectorAll('h3.r a');
                return Array.prototype.map.call(links, function(e) {
                    return e.getAttribute('href');
                });
            });
        });
        spooky.run(function(){
            this.emit('hello', links.join("\n"));
        });
    });
$ node spooky-google.js
attempting to fetch form element from selector: 'form[action="/search"]'
Set "q" field value to 東京特許許可局
submitting form to /search, HTTP GET
unable to submit form
https://ja.wikipedia.org/wiki/%E6%9D%B1%E4%BA%AC%E7%89%B9%E8%A8%B1%E8%A8%B1%E5%8F%AF%E5%B1%80
http://www.tokkyonavi.com/qanda/basic/%E6%9D%B1%E4%BA%AC%E7%89%B9%E8%A8%B1%E8%A8%B1%E5%8F%AF%E5%B1%80.html
http://ja.uncyclopedia.info/wiki/%E6%9D%B1%E4%BA%AC%E7%89%B9%E8%A8%B1%E8%A8%B1%E5%8F%AF%E5%B1%80
http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q12442622

めちゃくちゃはまりました。。。

1
0
2

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?