节点js cheerio解析html

时间:2017-10-05 16:35:11

标签: javascript node.js parsing cheerio

所以我有一个问题,我无法获得电影的链接,我想要为#34;人们也喜欢"部分,它显示了类似的电影。虽然因为有一个角色部分,但我无法在某些电影上获得该页面

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

http://www.imdb.com/find?ref_=nv_sr_fn&q=Lucifer&s=all

1 个答案:

答案 0 :(得分:1)

有一个像这样的标签名称&#34; tt&#34; <a name="tt"></a> 您可以使用此选择器来获取所需的部分标记

var endurl = $('a[name=tt]').parent().parent().find(".findSection .findList .findResult .result_text a").attr("href");