LoginSignup
6
6

More than 5 years have passed since last update.

PhantomJSで指定したURLのHTMLに含まれる自ドメインのリンクリストを返却する

Last updated at Posted at 2014-07-17

filterはIE8だと利用できないです。

links.js
var  page = require('webpage').create()
    ,system = require('system')
    ,ua, url;

if (system.args.length != 3) {
    console.log('[Error] Invalid Argument...');
    phantom.exit();
} else {
    url = system.args[1];
    ua = system.args[2];

    page.settings.userAgent = ua;
    page.open(url, function (status) {
        if (status !== 'success') {
            console.log('[Error] Unable to load the [' + url + '] ...');
        } else {
            var ret = page.evaluate(function () {
                var aTag = document.links;
                var aTagList = [];
                var domain = location.href.match(/^[httpsfile]+:\/{2,3}([0-9a-z\.\-:]+?):?[0-9]*?\//i)[1];

                for (var i = 0, len = aTag.length; i < len; i++) {
                    if (aTag[i].href.indexOf(domain) == -1) continue;
                    aTagList.push(aTag[i].href);
                }

                var aTagUniqueList = aTagList.filter(function (x, index, self) {
                    return self.indexOf(x) === index;
                });
                return aTagUniqueList.join("\n");
            });
            console.log(ret);
        }
        phantom.exit();
    });
}
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