如何获取 cheerio 检索的所有标题名称?
How to get all heading names retrieved by cheerio?
我有以下代码,旨在输出 cheerio 从特定 html 页面检索到的所有标题名称。
const cheerio = require('cheerio');
const rp = require('request-promise');
async function run() {
const options = {
uri: '<SOME_URL>',
resolveWithFullResponse: true,
transform: (body) => {
return cheerio.load(body);
}
}
try{
const $ = await rp(options);
$("h1, h2, h3, h4, h5, h6").map(e => {
console.log(e);
});
}catch(e){
console.log(e);
}
}
run();
但是上面代码的输出类似于
0
1
2
...
我尝试将 console.log(e)
更改为 e.attr('name')
,然后它 returns 我出错了
TypeError: e.attr is not a function
您的问题是 $().map
将索引作为第一个参数,将元素作为第二个参数。
我猜你需要这个:
const cheerio = require('cheerio');
const rp = require('request-promise');
const uri = 'http://www.somesite.com';
async function run() {
const options = {
uri,
resolveWithFullResponse: true,
transform: (body) => {
return cheerio.load(body);
}
}
try{
const $ = await rp(options);
$("h1, h2, h3, h4, h5, h6").map((_,element) => {
console.log($(element).html()) // just output the content too to check everything is alright
console.log(element.name);
});
}catch(e){
console.log(e);
}
}
run();
我有以下代码,旨在输出 cheerio 从特定 html 页面检索到的所有标题名称。
const cheerio = require('cheerio');
const rp = require('request-promise');
async function run() {
const options = {
uri: '<SOME_URL>',
resolveWithFullResponse: true,
transform: (body) => {
return cheerio.load(body);
}
}
try{
const $ = await rp(options);
$("h1, h2, h3, h4, h5, h6").map(e => {
console.log(e);
});
}catch(e){
console.log(e);
}
}
run();
但是上面代码的输出类似于
0
1
2
...
我尝试将 console.log(e)
更改为 e.attr('name')
,然后它 returns 我出错了
TypeError: e.attr is not a function
您的问题是 $().map
将索引作为第一个参数,将元素作为第二个参数。
我猜你需要这个:
const cheerio = require('cheerio');
const rp = require('request-promise');
const uri = 'http://www.somesite.com';
async function run() {
const options = {
uri,
resolveWithFullResponse: true,
transform: (body) => {
return cheerio.load(body);
}
}
try{
const $ = await rp(options);
$("h1, h2, h3, h4, h5, h6").map((_,element) => {
console.log($(element).html()) // just output the content too to check everything is alright
console.log(element.name);
});
}catch(e){
console.log(e);
}
}
run();