我正在开发网络抓取工具,并且已经成功地能够从该站点检索信息。 唯一的问题是,我需要有关如何传递多个命令行参数的帮助,就我而言,我想传递多个音乐艺术家以返回在网站上找到的内容,这将允许返回更多数据。
我尝试使用for循环来获取命令行参数的数量。然后尝试使用foreach循环。
var request = require('request');
var cheerio = require('cheerio');
const args = process.argv.slice(2);
//request function that uses three parameters were we check for the error and response type
request(' https://www.billboard.com/charts/rap-song', function (error, response, html){
if(!error && response.statusCode === 200) {
var $ = cheerio.load(html);
var results = $('div').find('.chart-list-item__artist:contains("'+ artist +'")').each(function () {
// console.log($(this).text());
}).text();
});
也许是因为包含操作符只能处理一个命令行参数?或仅获得指定的第一位艺术家。
Expected output: node artists.js "Post Malone" "Lil Baby"
----------------
Post Malone & Swae Lee
Post Malone
Lil Baby
Yo Gotti Featuring Lil Baby
Actual Output: node artists.js "Post Malone" "Lil Baby"
--------------
Post Malone & Swae Lee
Post Malone
答案 0 :(得分:0)
据我所知,您应该迭代艺术家并检查每个艺术家的匹配项,而不是仅在匹配一位艺术家之后对元素进行迭代。
我认为这样-基于您的拥有
var request = require('request');
var cheerio = require('cheerio');
const args = process.argv.slice(2);
//request function that uses three parameters were we check for the error and response type
request(' https://www.billboard.com/charts/rap-song', function (error, response, html){
if(!error && response.statusCode === 200) {
var $ = cheerio.load(html);
var results = [];
args.forEach(artist => {
$('div').find('.chart-list-item__artist:contains("'+ artist +'")').each(function () {
// console.log($(this).text());
results.push(this); // push current element into results
}).text();
});
});
})