NodeJS Cheerio 解析 html
Node js cheerio parsing html
所以我遇到了一个问题,我无法将 link 获取到我想为 "People also liked" 部分显示类似电影的电影。不过有些电影我看不到那个页面,因为有角色部分
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)
})
Lucifer
Gotham
有一个这样的标签,名称为 "tt" <a name="tt"></a>
您可以使用此选择器获取所需的部分标记
var endurl = $('a[name=tt]').parent().parent().find(".findSection .findList .findResult .result_text a").attr("href");
所以我遇到了一个问题,我无法将 link 获取到我想为 "People also liked" 部分显示类似电影的电影。不过有些电影我看不到那个页面,因为有角色部分
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)
})
Lucifer
Gotham
有一个这样的标签,名称为 "tt" <a name="tt"></a>
您可以使用此选择器获取所需的部分标记
var endurl = $('a[name=tt]').parent().parent().find(".findSection .findList .findResult .result_text a").attr("href");