从 html 中提取 js 和 css 文件的网址? (使用 node.js)
extract urls for js & css files from html? (using node.js)
我想要一个来自 html 字符串的 url 数组,尽管只来自以下标签:
- link href="http://example.com/foo.css"
- 脚本源="http://example.com/foo.js"
我想要这些 URL,这样我就可以将它们放入应用缓存清单文件中。我使用 appcache 清单生成器,但它只分析我在本地提供的静态文件。它运行良好,但它不会自动包含我在 html.
中包含的外部静态 js/css 文件
我希望能够使用 node.js 解析 html 字符串。
您可以使用 cheerio。它是节点的核心 jQuery 的实现。
例如:
var cheerio = require('cheerio'),
request = require('request');
request('http://www.whosebug.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
var linkHrefs = $('link').map(function(i) {
return $(this).attr('href');
}).get();
var scriptSrcs = $('script').map(function(i) {
return $(this).attr('src');
}).get();
console.log("links:");
console.log(linkHrefs);
console.log("scripts:");
console.log(scriptSrcs);
}
});
输出:
Victors-MacBook-Pro:a kohl$ node test.js
links:
[ '//cdn.sstatic.net/Whosebug/img/favicon.ico?v=6cd6089ee7f6',
'//cdn.sstatic.net/Whosebug/img/apple-touch-icon.png?v=41f6e13ade69',
'/opensearch.xml',
'//cdn.sstatic.net/Whosebug/all.css?v=317033db9646',
'/feeds' ]
scripts:
[ '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
'//cdn.sstatic.net/Js/stub.en.js?v=e3a448574e16' ]
我想要一个来自 html 字符串的 url 数组,尽管只来自以下标签:
- link href="http://example.com/foo.css"
- 脚本源="http://example.com/foo.js"
我想要这些 URL,这样我就可以将它们放入应用缓存清单文件中。我使用 appcache 清单生成器,但它只分析我在本地提供的静态文件。它运行良好,但它不会自动包含我在 html.
中包含的外部静态 js/css 文件我希望能够使用 node.js 解析 html 字符串。
您可以使用 cheerio。它是节点的核心 jQuery 的实现。
例如:
var cheerio = require('cheerio'),
request = require('request');
request('http://www.whosebug.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
var linkHrefs = $('link').map(function(i) {
return $(this).attr('href');
}).get();
var scriptSrcs = $('script').map(function(i) {
return $(this).attr('src');
}).get();
console.log("links:");
console.log(linkHrefs);
console.log("scripts:");
console.log(scriptSrcs);
}
});
输出:
Victors-MacBook-Pro:a kohl$ node test.js
links:
[ '//cdn.sstatic.net/Whosebug/img/favicon.ico?v=6cd6089ee7f6',
'//cdn.sstatic.net/Whosebug/img/apple-touch-icon.png?v=41f6e13ade69',
'/opensearch.xml',
'//cdn.sstatic.net/Whosebug/all.css?v=317033db9646',
'/feeds' ]
scripts:
[ '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
'//cdn.sstatic.net/Js/stub.en.js?v=e3a448574e16' ]