节点js cheerio解析html
问题描述:
所以我有一个问题,我无法获得电影的链接,我想为“人们也喜欢”部分刮到它显示你类似的电影。我不能太上一些电影该页面,但因为有一个人物部分节点js cheerio解析html
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)
})
与数据的数组
答
有这样的AA标签名称为“TT” <a name="tt"></a>
您可以使用此选择,以获得所需的部分标签
var endurl = $('a[name=tt]').parent().parent().find(".findSection .findList .findResult .result_text a").attr("href");
我不明白,我怎么会用这去我的标签 –
看看我最新的编辑,并用新的替换你的endurl变量。 –