【问题标题】:Node js cheerio parsing htmlNode js Cheerio 解析 html
【发布时间】: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)
})

Lucifer

Gotham

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

【问题讨论】:

    标签: javascript node.js parsing cheerio


    【解决方案1】:

    有一个类似这样的标签,名称为“tt”<a name="tt"></a> 您可以使用此选择器获取所需的部分标签

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

    【讨论】:

    • 我不明白如何使用它来访问我的标签
    • 查看我的最新编辑并将您的 endurl 变量替换为新变量。
    猜你喜欢
    • 2013-08-09
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 2015-10-07
    • 1970-01-01
    相关资源
    最近更新 更多