2017-05-17 52 views
0

我有這樣的HTML:Cheerio,標籤之間刪除值

<strong>delete1</strong> : 
16X<br> 
<strong>delete2</strong> 
: 16X<br> 
<strong>delete3</strong> 

需要刪除的標籤和標籤之間的所有文本。怎麼做? Node.js + cheerio,網頁抓取。

回答

0

在Cheerio的操作特徵中有remove方法。 所以你可以刪除這個內容中的所有元素,那麼你將只獲得沒有任何子元素和文本的文本。

下面是一個例子,用JQuery來做這件事。

$(document).ready(function(){ 
 
    var $elm = $('#demo-container'); 
 

 
    // Remove all child elements then their contents will be gone. 
 
    $elm.find('*').remove(); 
 
    console.log('Contents without child elements',$elm.html()); 
 

 
    // Beyond that you could split text with colon and get each values 
 
    console.log('Each line as array',$elm.html().replace(/\s/g, '').split(':')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="demo-container"> 
 
    <strong>delete1</strong> : 
 
    16X<br> 
 
    <strong>delete2</strong> 
 
    : 16X<br> 
 
    <strong>delete3</strong> 
 
</div>

0

您可以使用cheerio得到父的HTML與replace刪除不必要的內容。您可以使用X-ray並使用filters選項。

相關問題