How to use JQuery selectors in Node.js

后端 未结 4 1529
梦谈多话
梦谈多话 2021-02-06 05:16

I\'m trying to extract email info from HTML files in my hard drive.

If I load the file in firefox and run jQuerify bookmarklet I can use successfully the following selec

4条回答
  •  天涯浪人
    2021-02-06 05:37

    jsdom supports jQuery with "official" way.

    jsdom.env(string, [scripts], [config], callback);
    

    Simple code:

    var jsdom = require("jsdom"),
    fs = require('fs');
    
    fs.readFile('file_1.html', 'utf-8', function (err, data) {
        if (err) {
            throw err;
        }
        jsdom.env(data, ["http://code.jquery.com/jquery.js"], function (errors, window) {
            var $ = window.$;
            $("a.iEmail").each(function() {
                console.log(this.href)
            });
        })
    
    }
    

    https://github.com/tmpvar/jsdom#easymode

提交回复
热议问题