2015-07-03 65 views

回答

1

这应该工作,只是npm install request cheerio。请注意,这是异步的,所以你可能想把它放在承诺或类似的地方。

var cheerio = require("cheerio"); 
var request = require("request"); 

var baseUrl = "https://play.google.com/store/apps/details?id="; 
var app = "com.spotify.music"; 

function getScreenShots(html) { 
    var $ = cheerio.load(html); 
    var $img = $(".thumbnails img.screenshot"); 
    var images = []; 
    $img.each(function() { 
     images.push($(this).attr("src")); 
    }); 
    console.log(images); 
} 

function getReviews(html) { 
    var $ = cheerio.load(html); 
    var $allReviews = $(".single-review"); 
    var reviews = []; 
    $allReviews.each(function() { 
     var rating = $(".review-info-star-rating > div", $(this)).attr("aria-label"); 
     var review = { 
      "author": $(".author-name", $(this)).text(), 
      "date": $(".review-date", $(this)).text(), 
      "rating": rating.match(/([12345]){1}/)[0] + "/5", 
      "comment": $(".review-body", $(this)).text() 
     } 
     reviews.push(review); 
    }); 
    console.log(reviews); 
} 

request(baseUrl + app, function(error, response, body) { 
    getScreenShots(body); 
    getReviews(body); 
}); 
+0

以同样的方式,我们可以得到用户评论? – Ankita

+0

当然,我已经更新了我的答案以及审查示例。 – hampusohlsson

相关问题