我想用node.js从网页上刮下头,但我不知道如何。我可以通过像这样的Cheerio来获得所有的身体:
request(webUrl, function(err, resp, body){
if(!err && resp.statusCode == 200) {
var $ = cheerio.load(body);
//Getting all the links 'a' from the webpage
$('a').each(function(){
//Getting the href attribute from the 'a' link
var url = $(this).attr('href');
//We keep the link only if it is the same root (in order to avoid the 'undefined' links and the subdomains or outside links (like social media links))
if(url != undefined && url[0] == '/') {
//We add the domain name to the url we got in order to have the full
url = websiteUrl + url;
urls.push(url);
}
});
console.log(urls);
}
});
,但不可能用这种方法掌握头部。我尝试了一下,但它仅给了我的身体脚本,而不是标题中的脚本:
request(webUrl, function(err, resp, body){
if(!err && resp.statusCode == 200) {
var $ = cheerio.load(body);
$('script').each(function(){
//Getting the href attribute from the 'a' link
var url = $(this).attr('src');
console.log(url);
if(url != undefined) {
wowo.push(url);
}
});
console.log(wowo);
}
});
有人可以帮我吗?:'(
看一下htmlparser2
库(Cheerio内部使用它)。
这是现场使用的节目。