html抓取,获取具有相同名称的不同值

时间:2017-11-10 12:27:31

标签: javascript node.js web-scraping request cheerio

因为网站没有api我想从csgo统计数据,我试图从网站上刮,我知道如何加载网站等,如打印主页,但我无法想象如何加载特定值,因为它们中的大多数具有您将在下面看到的相同名称。我试图获得win%的值和kd的值,但这两个类都被称为" stats-stat",我能做到这一点吗?

我的代码是:

var UR_L = "http://csgo.tracker.network/profile/blind_snip"; 

request(UR_L, function(err, resp, body){
  var $ = cheerio.load(body);
  console.log(body)
})

enter image description here

更新:我的工作代码:

 var UR_L = "http://csgo.tracker.network/profile/blind_snip"; 

 request(UR_L, function(err, resp, body){

     $ = cheerio.load(body);
     var ele = $('.stats-stat').eq(13).text();
       ele2 = $.parseHTML(ele)[0].data;
       console.log(ele2);


       //0 = KD
       //1 = win%
       //2 = games
       //3 = wins
       //4 = headshots
       //5 = money earned
       //6 = score
       //7 = kills
       //8 = Deaths
       //9 = MVP
       //10 = time played
       //11 = Rounds played
       //12 = rounds won
       //13 = bombs set
       //14 = bombs defused
       //15 = hostages rescued
  })

谢谢大家

1 个答案:

答案 0 :(得分:0)

我得到了字符串值,你可以使用正则表达式获得你需要的部分。如果你只是控制掉ele,你会看到它以/ n / n开头。还要注意我正在使用.eq并选择我想要的索引。你可以改为循环查询选择器。

request(UR_L, function(err, resp, body){

  $ = cheerio.load(body);
  var ele = $('.stats-stat').eq(1).text();
    console.log($.parseHTML(ele)[0].data);

})