使用 cheerio 提取元素
Extract elements using cheerio
我正在尝试编写 nodejs 代码,将用户的参数作为歌手的名字,然后转到 https://www.billboard.com/charts/rap-song
中指定的歌手页面,并打印他们所有歌曲的名字
例如,如果我 运行: Node Singer.js Drake
,它将转到 drake 页面,并打印他的所有歌曲,
我不知道该怎么做,谁能帮帮我。
var request = require('request');
var cheerio = require('cheerio');
const args = process.argv;
request('https://www.billboard.com/charts/rap-song', function(error,response,html){
if(!error && response.statusCode == 200){
var $ = cheerio.load(html);
$('a.chart-row__artist').each(function(i, element) {
if(i%2==1 && i<20){
console.log($(this).text());
}
});
}
});
使用这个
var url = "https://www.billboard.com/music/" + singerName;
request({
uri: url,
timeout: 120000
}, function (error, response, body) {
if (typeof body === 'undefined') {
console.log("error");
} else {
var $ = cheerio.load(html);
$('a.chart-row__artist').each(function(i, element) {
if(i%2==1 && i<20){
console.log(element);
}
});
}
});
使用正确的 Url 进行请求,然后使用 Cheerio
select 您想要的正确元素。我不检查网站。
我正在尝试编写 nodejs 代码,将用户的参数作为歌手的名字,然后转到 https://www.billboard.com/charts/rap-song
中指定的歌手页面,并打印他们所有歌曲的名字
例如,如果我 运行: Node Singer.js Drake
,它将转到 drake 页面,并打印他的所有歌曲,
我不知道该怎么做,谁能帮帮我。
var request = require('request');
var cheerio = require('cheerio');
const args = process.argv;
request('https://www.billboard.com/charts/rap-song', function(error,response,html){
if(!error && response.statusCode == 200){
var $ = cheerio.load(html);
$('a.chart-row__artist').each(function(i, element) {
if(i%2==1 && i<20){
console.log($(this).text());
}
});
}
});
使用这个
var url = "https://www.billboard.com/music/" + singerName;
request({
uri: url,
timeout: 120000
}, function (error, response, body) {
if (typeof body === 'undefined') {
console.log("error");
} else {
var $ = cheerio.load(html);
$('a.chart-row__artist').each(function(i, element) {
if(i%2==1 && i<20){
console.log(element);
}
});
}
});
使用正确的 Url 进行请求,然后使用 Cheerio
select 您想要的正确元素。我不检查网站。