简体   繁体   English

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

[英]html scraping, getting different values with the same name

since there is no api for the website which i would like to get csgo stats from, i am attempting to scrape from the website, i know how to load the website and such, like print the main page, but i cannot figure out how to load specific values because most of them have the same name as you will see below. 因为没有网站的api,我想从中获取csgo的统计信息,所以我试图从该网站上抓取,我知道如何加载该网站,例如打印主页,但是我不知道该怎么做。加载特定值,因为其中大多数具有与您将在下面看到的相同的名称。 i am attempting to get the value of win% and the value of kd but both of the classes are called "stats-stat", any way i can do this? 我正在尝试获取win%的值和kd的值,但是两个类都称为“ stats-stat”,我可以这样做吗?

my code is: 我的代码是:

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

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

在此处输入图片说明

UPDATE: my working code: 更新:我的工作代码:

 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
  })

Thanks people 谢谢大家

I got as far as getting the string value, you could use a regex to get the part you need. 就字符串值而言,我可以使用正则表达式来获取所需的部分。 If you just console out ele, you'll see it starts with /n/n. 如果您只是调试ele,则会看到它以/ n / n开头。 Also notice that i am using .eq and selecting the index i want. 另请注意,我正在使用.eq并选择所需的索引。 you could loop over the query selector instead. 您可以改为遍历查询选择器。

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

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

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM