Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
6
Help us understand the problem. What is going on with this article?
@uiu

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

More than 5 years have passed since last update.

この記事に書いてあるように、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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
uiu

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
6
Help us understand the problem. What is going on with this article?