2
var page = require('webpage').create();
page.open('http://www.alfeker.net/', function () {
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js",
        function () {
            try {
                var b = [];
                b = page.evaluate(function () {
                    $("a.titr").each(function () {
                        return b = $(this).text();
                    });
                });
            } catch (e) {
                console.log(e);
            }
            console.log(b);
        });
});
4

1 回答 1

2
var page = require('webpage').create();
var fs=require('fs');
var system = require('system');
var url = 'http://www.google.com/';
page.open(url, function(status) {
    var links = page.evaluate(function() {
        return [].map.call(document.querySelectorAll('a.gbzt'), function(link) {
            return link.getAttribute('href');
        });
    });
    var _urls = links.join('\n');
    try
    {
        fs.write('/home/JIM/Documents/phantomjs-1.9.1-linux-x86_64/bin/MEFile/Log.txt', _urls , 'w');
    }
    catch (e)
    {
        console.log(e);
    }
    if (system.args.length < 2) {
        console.log("Usage: readFile.js FILE");
        phantom.exit(1);
    }
    var content = '',
        f = null,
        lines = null,
        eol = system.os.name == 'windows' ? "\r\n" : "\n";
    try {
        f = fs.open(system.args[1], "r");
        content = f.read();
    } catch (e) {
        console.log(e);
    }
    if (f) {
        f.close();
    }
    if (content) {
        lines = content.split(eol);
        for (var i = 0, len = lines.length; i < len; i++) {
            console.log(lines[i]);
        }
    }  
    phantom.exit();
    });
于 2013-08-01T10:07:58.913 回答