在另一个请求中使用request.js发出请求

时间:2018-03-16 18:10:42

标签: node.js telegram telegram-bot cheerio requestjs

我正在创建一个内联电报机器人,但我有问题 我的问题是我在另一个请求中有一个请求,我希望内部请求的结果进入外部请求,我的代码是:

request(urlToSearch, function (error, response, body) {
  var $ = cheerio.load(body);
    $('div.Title' , 'div.Body').each(function(){
        var title = $(this).children().first().text();
        var url = $(this).parent().attr('href');
        var episodes = $(this).parent().next().children().next().attr('content');
        request(url, function (error, response, body) {
          var $ = cheerio.load(body);
          var json = { title : "", story : "", rating : "", image: "", japtitle: "",episodes: "",genre:"",type:""};
          var title, story, rating, image, japtitle, episodes, genre,type;
          $('.Post_Content').filter(function(){
                var data = $(this);
                japtitle = data.children().find($('.Dic_Anime div:nth-child(3)')).clone().children().remove().end().text();
                genre = data.children().find($('.Dic_Anime div:nth-child(13)')).text();
                type = data.children().find($('.Dic_Anime div:nth-child(6) a')).text();
                episodes = data.children().find($('.Dic_Anime div:nth-child(7)')).clone().children().remove().end().text();
                image = data.children().next().children().first().attr('src');
                rating = data.find($('.col-md-7')).find($('.Anime_Dic')).find($('.tab-content')).children().first().children().first().children().first().text()
                story = data.find($('.col-md-7')).find($('.Anime_Dic')).find($('.tab-content')).children().find($('.description_Anime')).find('span[itemprop="description"]').clone().children().remove().end().text();
                json.rating = rating.replace( /^\D+/g, '');
                json.image = image;
                json.type = type.trim();
                json.episodes = episodes.trim();
                json.japtitle = japtitle.trim();
                json.story = story.trim();
            });
          json.genre = genre.replace("ژانرها :"," ").trim();
          $('.Post_Content .Title a > h1').filter(function(){
            title = $(this).text();
            json.title = title.replace(/[^a-z0-9]/gmi, " ").replace(/\s+/g, " ");
          })
        });
        var metadata = {
            name : title,
            url : url,
            episodeCount : episodes,
            };
        urls.push(metadata);
    })
    console.log(urls)

    // for(var i = urls.length - (urls.length+5); i < 0; i++) {
    //     urls.splice(i, 1);
    // }
    // for(var i = 0;i < urls.length;i++){
    //     var resultsObject = {
    //         'type' : 'article',
    //         'id' : '8795486218'+i+urls[i].name,
    //         'title' : urls[i].name,
    //         'input_message_content' : {message_text:urls[i].things.japtitle}
    //     }
    //     results.push(resultsObject);
    // }
    // bot.answerInlineQuery(msg.id, results,{cache_time:1});

});

我想做的只是json变量到元数据变量,我可以将它用于结果

0 个答案:

没有答案