2016-08-16 3 views
0

RedditをNodejs、Horseman、Cheeriosで削っています。Reddit投稿のタイトルは配列に保存されていますが、最初の配列には見出しがたくさん含まれています

var Horseman = require('node-horseman'); 
    var cheerio = require('cheerio'); 
    var horseman = new Horseman(); 

    var parsedResults = []; 

    horseman 
     .open('http://reddit.com/') 
     .html() 
     .then(function(html) { 
      var $ = cheerio.load(html); 
      var headlines = $('p.title'); 
      var metadata = { 
       headline: headlines.text() 
      }; 
      parsedResults.push(metadata); 
      headlines.each(function() { 
       //console.log($(this).text()); 
       parsedResults.push($(this).text()); 
      }); 
      console.log(parsedResults); 
     horseman.close(); 
     }); 

スクレーパー作品が、私は、配列console.log(parsedResults);を印刷するたびに、他のすべてが一つだけが含まれていながら、最初の結果だけではなく、1の他の配列データスロット内の見出しのリスト全体が含まれているようです。コードはmetadata.headline$('p.title').text()の値を設定していることを

[ { headline: 'TIL that 75% of people in New York had a STD during the 19th century (en.wikipedia.org)ROGUE ONE: A NEW HOPE STORY - Official International Trailer #2 (2016) Sci-Fi Movie HD (youtube.com)What is something you remember happening in school that could NEVER happen today? (self.AskReddit)Hyperloop One could build its first commercial route in Dubaiarticle (engadget.com)I thank Texas Instruments Ti-84 CE for an extremely small screen, so I can play games on the calculator without my teacher noticing. (self.Showerthoughts)Bill Clinton was asked to fly to North Korea to bring back the two young reporters because of a misunderstanding.Politics (self.Jokes)We like cats. Do you? (i.imgur.com)Need new game play? any ideas? (self.gaming)An Olympic message to the Brazilian Bob Costas: Shut it! (hosted.ap.org)Hosting The Olympics Is A Terrible Investment (fivethirtyeight.com)My Fellow Reddit Gamers! Gaming Season is Upon Us and We thought It Would Be Awesome For You to Enjoy a NeuTech® 4000DPI Multi-Color Backlit Professional Wired Gaming Mouse For $0 - Just Pay the Shipping! (deal-catchers.com)"Okay, I think I proved my point." (i.imgur.com)Crazy lighting bolt in Tampa (i.imgur.com)Its picture day! (i.imgur.com)"That was in if you want to challenge it": Classy tennis player tells his opponent to challenge a serve called out. (youtube.com)my gorgeous mom at 17 in her church\'s chicken uniform (1971) my dad would walk 5 miles from his job at taco bell to walk her home (i.reddituploads.com)ELI5: How does mental or emotional stress manifest with different physical symptoms (i.e. pimples, nausea, panic attacks, etc.)?Biology (self.explainlikeimfive)Mark Cavendish causes crash during racePicture/Video (gfycat.com)Caskets are coming out of the ground near baton rouge (imgur.com)TIL there are only three northern white rhinos left on earth, the only male has 24x7 armed guards, and there is a gofundme to raise half a million bucks to develop rhino IVF to keep the species alive (theeastafrican.co.ke)You maybe cool but you\'re not Charlize Theron siting on a lawn chair in a space suit near a volcano cool. (jetgoodson.net)Diving gold medalist Shi Tingmao and bronze medalist Tania Cagnotto are exhilarated as silver medalist He Zi receives marriage proposal.Diving (i.imgur.com)NASA: Last Month Was Earth\'s Hottest in Recorded History (abcnews.go.com)Welcome to No Man\'s Sky (vid.pr0gramm.com)The Olympics should have a \'For Fun\' section at the end of all the games so all the athletes can try different sports. (self.Showerthoughts)LPT: If you have the desire (and funds) to travel, but no one to travel with, go anywayTraveling (self.LifeProTips)Scientists take big step toward recreating primordial \'RNA world\' of 4 billion years agoEarth Science (phys.org)Bikini Bottom Super Band - Sweet Victory [Rock]music streaming (m.youtube.com)If a woman sleeps with 10 men she\'s a slut, but if a man does it.......... (self.Jokes)Found a surprisingly detailed face in my new water bottle (imgur.com)Girl uses Fifty Shades of Grey as basis for false rape claims against her father (independent.co.uk)[OC] U.S. Lags behind in the race of Fiber connectivityOC (mykotori.com)China has launched the world\'s first quantum science satellite to test quantum communication and entanglement over long distances (gbtimes.com)Made my own OreosImage (imgur.com)Dad Accused of Raping Daughter Saved by Mention of \'Fifty Shades of Grey\' (fox59.com)Cornea cells \'successfully\' grown and implanted to cure blindnessarticle (abc.net.au)' }, 
    'TIL that 75% of people in New York had a STD during the 19th century (en.wikipedia.org)', 
    'ROGUE ONE: A NEW HOPE STORY - Official International Trailer #2 (2016) Sci-Fi Movie HD (youtube.com)', 
    'What is something you remember happening in school that could NEVER happen today? (self.AskReddit)', 
    'Hyperloop One could build its first commercial route in Dubaiarticle (engadget.com)', 
    'I thank Texas Instruments Ti-84 CE for an extremely small screen, so I can play games on the calculator without my teacher noticing. (self.Showerthoughts)', 
    'Bill Clinton was asked to fly to North Korea to bring back the two young reporters because of a misunderstanding.Politics (self.Jokes)', 
    'We like cats. Do you? (i.imgur.com)', 
    'Need new game play? any ideas? (self.gaming)', 
    'An Olympic message to the Brazilian Bob Costas: Shut it! (hosted.ap.org)', 
    'Hosting The Olympics Is A Terrible Investment (fivethirtyeight.com)', 
    'My Fellow Reddit Gamers! Gaming Season is Upon Us and We thought It Would Be Awesome For You to Enjoy a NeuTech® 4000DPI Multi-Color Backlit Professional Wired Gaming Mouse For $0 - Just Pay the Shipping! (deal-catchers.com)', 
    '"Okay, I think I proved my point." (i.imgur.com)', 
    'Crazy lighting bolt in Tampa (i.imgur.com)', 
    'Its picture day! (i.imgur.com)', 
    '"That was in if you want to challenge it": Classy tennis player tells his opponent to challenge a serve called out. (youtube.com)', 
    'my gorgeous mom at 17 in her church\'s chicken uniform (1971) my dad would walk 5 miles from his job at taco bell to walk her home (i.reddituploads.com)', 
    'ELI5: How does mental or emotional stress manifest with different physical symptoms (i.e. pimples, nausea, panic attacks, etc.)?Biology (self.explainlikeimfive)', 
    'Mark Cavendish causes crash during racePicture/Video (gfycat.com)', 
    'Caskets are coming out of the ground near baton rouge (imgur.com)', 
    'TIL there are only three northern white rhinos left on earth, the only male has 24x7 armed guards, and there is a gofundme to raise half a million bucks to develop rhino IVF to keep the species alive (theeastafrican.co.ke)', 
    'You maybe cool but you\'re not Charlize Theron siting on a lawn chair in a space suit near a volcano cool. (jetgoodson.net)', 
    'Diving gold medalist Shi Tingmao and bronze medalist Tania Cagnotto are exhilarated as silver medalist He Zi receives marriage proposal.Diving (i.imgur.com)', 
    'NASA: Last Month Was Earth\'s Hottest in Recorded History (abcnews.go.com)', 
    'Welcome to No Man\'s Sky (vid.pr0gramm.com)', 
    'The Olympics should have a \'For Fun\' section at the end of all the games so all the athletes can try different sports. (self.Showerthoughts)', 
    'LPT: If you have the desire (and funds) to travel, but no one to travel with, go anywayTraveling (self.LifeProTips)', 
    'Scientists take big step toward recreating primordial \'RNA world\' of 4 billion years agoEarth Science (phys.org)', 
    'Bikini Bottom Super Band - Sweet Victory [Rock]music streaming (m.youtube.com)', 
    'If a woman sleeps with 10 men she\'s a slut, but if a man does it.......... (self.Jokes)', 
    'Found a surprisingly detailed face in my new water bottle (imgur.com)', 
    'Girl uses Fifty Shades of Grey as basis for false rape claims against her father (independent.co.uk)', 
    '[OC] U.S. Lags behind in the race of Fiber connectivityOC (mykotori.com)', 
    'China has launched the world\'s first quantum science satellite to test quantum communication and entanglement over long distances (gbtimes.com)', 
    'Made my own OreosImage (imgur.com)', 
    'Dad Accused of Raping Daughter Saved by Mention of \'Fifty Shades of Grey\' (fox59.com)', 
    'Cornea cells \'successfully\' grown and implanted to cure blindnessarticle (abc.net.au)' ] 

答えて

1

原因です。今すぐ$('p.title')は、ページ上のすべてのタイトルのコレクションです。text() - コレクションのcheerioは、すべてのテキストを一緒に追加します。

次の行を削除して(メタデータを作成して結果にプッシュする)、それが機能します。

var metadata = { 
    headline: headlines.text() 
}; 
parsedResults.push(metadata); 
関連する問題