2017-10-26 1 views
-1

私は、記事とその説明を取り入れるJSでニューススクレーパーアプリを作成しています。JavaScriptで動的文字列の最後の文字を取得しようとしていますか?

以下はスクレーパーです。ただし、説明を読み込むときには、リンクからテキストとして「読ませてください」を引き込みます。私はまだ抜粋を引っ張りたいが、最後の部分にあるRead Moreを削除する:

app.get("/scraper", function(req, res) { 
    // Grabs the body of the html with request 
    request("https://techcrunch.com/", function(error, response, html) { 
    // Load into cheerio with $ as a shorthand selector 
    var $ = cheerio.load(html); 
    // Grabs the title, description, and link within the block-content class. 
    $(".block-content").each(function(i, element) { 

       // Save an empty result object 
       var result = {}; 

       // Saves them as properties of the result object 
       result.title = $(this).find(".post-title").children("a").text(); 
       result.link = $(this).find("a").children(".excerpt").attr("href"); 
       result.description =$(this).find(".excerpt").text(); 

       console.log(result); 

     if (result.title && result.link && result.description) { 

     // Creates a new entry using the article model 

     var entry = new Article(result); 

      // Saves that entry to the db 
      entry.save(function(err, doc, next) { 
       // Log any errors 
       if (err) { 
       console.log(err); 
       } 
      }); 
     } 
}); 

これはオブジェクトです。あなたは説明で "もっと読む"を見ることができます。 replaceを使用して

{ title: 'Snapcart raises $10M to shed light on consumer spending in emerging markets', 
    link: 'https://techcrunch.com/2017/10/25/snapcart-raises-10m/', 
    description: 'Taking on a giant like the $15 billion research firm Nielsen is no easy task. But tucked away in Southeast Asia, Snapcart is a two-year old company that is making progress by shining light on the black box that is consumer spending in emerging markets. Read More' } 
+1

質問をしてください。 –

答えて

0
result.description =$(this).find(".excerpt").text().replace(' Read More', ''); 

文字列からRead Moreを削除します。

関連する問題