この記事に書いてあるように、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']);