【发布时间】:2017-10-05 16:35:11
【问题描述】:
所以我有一个问题,我无法获取我想为“人们也喜欢”部分的电影的链接,它会向您展示相似的电影。不过因为有角色部分,所以我无法在某些电影上获得该页面
function findCommonMovies(movie, callback){
request('http://www.imdb.com/find?ref_=nv_sr_fn&q='+ movie +'&s=all', function (error, response, body) {
if (error){
return
}else{
var $ = cheerio.load(body);
var title = $(".result_text").first().text().split("(")[0].split(" ").join('')
var commonMovies = []
var endurl = $(".findSection .findList .findResult .result_text a").attr("href")
var test
request('http://www.imdb.com' + endurl, function (err, response, body) {
if (err){
console.log(err)
}else{
var $ = cheerio.load(body);
$(".rec_page .rec_item a img").each(function(){
var title = $(this).attr("title")
commonMovies.push(title)
});
}
callback(commonMovies)
});
}
});
}
findCommonMovies("Lucifer", function(common){
console.log(common)
})
将打印出一个空数组
findCommonMovies("Lucifer", function(common){
console.log(common)
})
将打印出一个包含数据的数组
findCommonMovies("Gotham", function(common){
console.log(common)
})
【问题讨论】:
标签: javascript node.js parsing cheerio