Intereting Posts

parsingNode.js中的数据

我有这个代码就在这里,我想parsingdata ,它应该得到位于url:部分的url: ,代码是:

 var search = require('youtube-search'); var opts = { maxResults: 3, startIndex: 1 }; search('deadmau5', opts, function(err, results) { if(err) return console.log(err); var title1 = results.indexOf("url:"); var title2 = results.indexOf("&feature"); var titl = results.substr(title1 + 7); var result = titl.substr(0, titl.indexOf("&feature")); console.log(result); }); 

但是我得到这个错误:

  var titl = results.substr(title1 + 7); ^ TypeError: Object [object Object],[object Object],[object Object],[object Object ],[object Object],[object Object],[object Object],[object Object],[object Object ],[object Object] has no method 'substr' at c:\Users\karim_000\desktop\iplogger.js:12:25 at c:\Users\karim_000\desktop\node_modules\youtube-search\index.js:42:9 at Parser.<anonymous> (c:\Users\karim_000\desktop\node_modules\youtube-searc h\node_modules\xml2js\lib\xml2js.js:255:20) at Parser.emit (events.js:95:17) at Object.saxParser.onclosetag (c:\Users\karim_000\desktop\node_modules\yout ube-search\node_modules\xml2js\lib\xml2js.js:225:24) at emit (c:\Users\karim_000\desktop\node_modules\youtube-search\node_modules \xml2js\node_modules\sax\lib\sax.js:615:33) at emitNode (c:\Users\karim_000\desktop\node_modules\youtube-search\node_mod ules\xml2js\node_modules\sax\lib\sax.js:620:3) at closeTag (c:\Users\karim_000\desktop\node_modules\youtube-search\node_mod ules\xml2js\node_modules\sax\lib\sax.js:861:5) at Object.write (c:\Users\karim_000\desktop\node_modules\youtube-search\node _modules\xml2js\node_modules\sax\lib\sax.js:1293:29) at Parser.exports.Parser.Parser.parseString (c:\Users\karim_000\desktop\node _modules\youtube-search\node_modules\xml2js\lib\xml2js.js:273:29) at Parser.parseString (c:\Users\karim_000\desktop\node_modules\youtube-searc h\node_modules\xml2js\lib\xml2js.js:6:61) 

我不完全知道我做错了什么,有什么见解或帮助? 提前致谢

Solutions Collecting From Web of "parsingNode.js中的数据"

这将logging您正在寻找的URL的每个子string,以便您可以进一步操作它。

 var search = require('youtube-search'); var opts = { maxResults: 3, startIndex: 1 }; search('deadmau5', opts, function(err, results) { if(err) return console.log(err); results.forEach(function(result) { console.log(result.url.substring(0, result.url.indexOf('&feature'))); }); }); 

这产生:

 http://www.youtube.com/watch?v=_9IBbMW2o_o http://www.youtube.com/watch?v=-O85-OXktNs http://www.youtube.com/watch?v=kWb-zs1L8Ss 

结果是一个对象的数组!

 search('deadmau5', opts, function(err, results) { if(err) return console.log(err); results.forEach(function (result) { console.log(result.url); }); }); 

这段代码会迭代你的数组并logging结果数组中每个对象的url-property!

正如Seth提到的,你会得到三个以上的search结果!

结果是一个Json数组,你不能使用string方法。 从Json获取你想要的元素,然后使用substr或其他方法

例如:在Json数组中的第一个元素的标题可以findvar title = results [0] ['title'];

我build议你阅读关于JSON