用Cheerio替换HTML节点

时间:2018-10-06 13:37:55

标签: javascript node.js dom cheerio

我正在使用Cheerio JS来简化一些古老的HTML代码并将其转换为HTML5。除其他外,我将替换一些类似于以下内容的重标记:

要替换的节点:

<div style="margin:20px; margin-top:5px; ">
    <div class="smallfont" style="margin-bottom:2px">Quote:</div>
    <table cellpadding="6" cellspacing="0" border="0" width="100%">
        <tbody>
            <tr>
                <td class="alt2" style="border:1px solid #999">
                    <div>
                        Originally Posted by <strong>Username</strong>
                    </div>
                    <div style="font-style:italic">Lorem ipsum dolor sit amet</div>
                </td>
            </tr>
        </tbody>
    </table>
</div>

转换后的输出应该看起来像这样:

<blockquote>Lorem ipsum dolor sit amet</blockquote>

这是我当前正在使用的代码当前代码:

$(`table[id^='post']`).each( (i, el) => {
    // Get the post
    let postBody = $(el).find(`div[id^='post_message_']`).html().trim();

    // Replace quotes with blockquotes
    cheerio.load(postBody)('div[style^="margin:20px; margin-top:5px; "]').each( (i, el) => {
        if ($(el).html().trim().startsWith('<div class="smallfont" style="margin-bottom:2px">Quote')) {
            let tbody = $(el).find('tbody > tr > td').html();
            let quote = $(el).find('tbody > tr > td > div');

            if (quote.html() && quote.text().trim().startsWith('Originally Posted by')) {
                let replacement = $('<blockquote>Hello</blockquote>');
                quote.parent().html().replace(quote.html(), replacement);
            }

            // Looks all good
            console.log($(el).html())
        }

        postBody = $(el).html();
    });
});

最后,在某些情况下还有更多HTML:

<div id="post_message_123456">
    As Username has previously written
    <br>
    <div style="margin:20px; margin-top:5px; ">
        <div class="smallfont" style="margin-bottom:2px">Quote:</div>
        <table cellpadding="6" cellspacing="0" border="0" width="100%">
            <tbody>
                <tr>
                    <td class="alt2" style="border:1px solid #999">

                        <div>
                            Originally Posted by <strong>Username</strong>
                        </div>
                        <div style="font-style:italic">Lorem ipsum dolor sit amet</div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <br>
    I think he has a point!
    <img src="smile-with-sunglasses.gif" />
</div>

替换本身似乎可以正常工作,console.log()语句的输出看起来很好。问题出在最后一行,我试图用替换内容替换原始内容。但是,postBody看起来像以前一样。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

像这样尝试:

let $ = cheerio.load(html)

$('.alt2 div:contains("Originally Posted by")').replaceWith('<blockquote>Lorem ipsum dolor sit amet</blockquote>')

console.log($.html())

答案 1 :(得分:1)

根据个人情况替换项目

这说明了如何使用安全的URL交换不安全的内容 作为一个有用的现实示例,并且对于大多数正常人而言,进行程序决策比使用正则表达式要容易得多。

const $ = cheerio.load(html)
// example replace all http:// with https://
$('img[src^="http://"]').replaceWith(function() {
  const src = $(this).attr('src')
  if (src.indexOf('s3.amazon.com')) {
    src = src.replace('s3.amazon.com', 'storage.azure')
  }
  return $(this).attr('src', src.replace('http://', 'https://'))
})