我在nodejs中使用kat.cr API和IMDB api编写API,我没有使用json.stringify cuz我在写haha XD时不知道它,无论如何,问题是当我循环遍历46到50中的代码时,响应保持不变这里是一个例子, 这是生成的json,
{
"MovieList": [{
"title": "Jurassic World",
"imdb": "tt0369610",
"poster_med": "http://ia.media-imdb.com/images/M/MV5BMTQ5MTE0MTk3Nl5BMl5BanBnXkFtZTgwMjczMzk2NTE@._V1_SX300.jpg",
"poster_big": "http://ia.media-imdb.com/images/M/MV5BMTQ5MTE0MTk3Nl5BMl5BanBnXkFtZTgwMjczMzk2NTE@._V1_SX300.jpg",
"genres": ["Action, Adventure, Sci-Fi"],
"items": [{
"torrent_magnet": "magnet:?xt=urn:btih:9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892&dn=maze+runner+the+scorch+trials+2015+hc+720p+hdrip+x264+shaanig&tr=udp%3A%2F%2Ftracker.publicbt.com%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337",
"torrent_seeds": "1262",
"torrent_peers": "1306",
"id": "9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892"
}]
}, {
"title": "San Andreas",
"imdb": "tt2126355",
"poster_med": "http://ia.media-imdb.com/images/M/MV5BNjI4MTgyOTAxOV5BMl5BanBnXkFtZTgwMjQwOTA4NTE@._V1_SX300.jpg",
"poster_big": "http://ia.media-imdb.com/images/M/MV5BNjI4MTgyOTAxOV5BMl5BanBnXkFtZTgwMjQwOTA4NTE@._V1_SX300.jpg",
"genres": ["Action, Drama, Thriller"],
"items": [{
"torrent_magnet": "magnet:?xt=urn:btih:9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892&dn=maze+runner+the+scorch+trials+2015+hc+720p+hdrip+x264+shaanig&tr=udp%3A%2F%2Ftracker.publicbt.com%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337",
"torrent_seeds": "1262",
"torrent_peers": "1306",
"id": "9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892"
}]
}]
}
以下是抓取工具代码:
var kat = require('kat-api');
var IMDb = require('imdb-scraper');
var movieTitle = require('movie-title');
var nameToImdb = require("name-to-imdb");
var movie = require('node-movie');
var fs = require('fs');
var util = require('util');
var log_file = fs.createWriteStream(__dirname + '/main.json', {
flags: 'w'
});
var log_stdout = process.stdout;
var config = '720p 2015'; //This is the line that should be changed if needed!
console.log = function(d) { //
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
var kat = require('kat-api');
kat.search({
query: config,
category: 'movies',
language: 'en'
}).then(function(response) {
var quotes = '"';
var startingOfJson = "{" + quotes + "MovieList" + quotes + ":" + "[";
var endingOfJson = "}";
var itemStart = quotes + "items" + quotes + ":" + "[{";
var itemEnd = "}]";
console.log(startingOfJson);
for (i = 0; i <= 20; i++) {
var titleForEverything = movieTitle(response.results[i].title);
movie(titleForEverything, function(err, data) {
console.log("{");
console.log(quotes + "title" + quotes + ":" + quotes + data.Title + quotes + ",");
console.log(quotes + "imdb" + quotes + ":" + quotes + data.imdbID + quotes + ",");
console.log(quotes + "poster_med" + quotes + ":" + quotes + data.Poster + quotes + ",");
console.log(quotes + "poster_big" + quotes + ":" + quotes + data.Poster + quotes + ",");
var genres = quotes + "genres" + quotes + ":" + "[" + quotes + data.Genre + quotes + "]" + ",";
console.log(genres);
console.log(itemStart);
console.log(quotes + "torrent_magnet" + quotes + ":" + quotes + response.results[i].magnet + quotes + ",");
console.log(quotes + "torrent_seeds" + quotes + ":" + quotes + response.results[i].seeds + quotes + ",");
console.log(quotes + "torrent_peers" + quotes + ":" + quotes + response.results[i].peers + quotes + ",");
console.log(quotes + "id" + quotes + ":" + quotes + response.results[i].hash + quotes);
console.log(itemEnd);
if (i == 20) {
console.log("}");
} else {
console.log("},")
}
});
}
}).catch(function(error) {
console.log('an error occured' + error);
});
console.log("]}");
你可以看到磁铁,种子,散列和同伴对于生成的所有结果保持不变!我该如何解决这个问题,为什么会发生这种情况?谢谢! :d
答案 0 :(得分:1)
你在一个循环中提交函数的经典错误,关闭循环索引print('print me please!')
;执行该函数时,i
已经具有其最终值。解决此问题的最简单方法是i
。