LoginSignup
6
6

More than 5 years have passed since last update.

jsdomでスクレイピングするときjQueryifyできなくて困る

Posted at

この記事に書いてあるように、jsdomはデフォルトでscriptタグを処理するため、スクレイピング用途で使うときはオプションを指定してscriptタグを無効にしたほうが便利。
しかし、そうするとjQueryifyが使えなくなってしまう。jQuery使えないのはだるいので、何とかする必要がある。

上の記事では自前でjQueryをごりごりロードしている。だけど、実は2行追加するだけで十分。

var document = jsdom.jsdom(data, null, { features: { ProcessExternalResources: false, FetchExternalResources: false }})
  , window = document.createWindow();

window.document.implementation.addFeature('FetchExternalResources', ['script']);
window.document.implementation.addFeature('ProcessExternalResources', ['script']);

jsdom.jQueryify(window, "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js", function (window, $) {
    // なんかやる
  });
});

結局

最初はscriptタグを無効にしておき、jQueryifyする直前で無理やりscriptタグを有効にする。

window.document.implementation.addFeature('FetchExternalResources', ['script']);
window.document.implementation.addFeature('ProcessExternalResources', ['script']);
6
6
0

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
6
6