【问题标题】:Replace HTML nodes with Cheerio用 Cheerio 替换 HTML 节点
【发布时间】:2019-03-11 18:55:00
【问题描述】:

我正在使用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 看起来就像以前一样。我做错了什么?

【问题讨论】:

  • 该 html 中没有 div[id^='post_message_'] 元素
  • @pguardiario 你怎么知道标记的样子?我只发布了我想替换的部分,并提到它一直工作到最后一行。
  • 请发布适用于您的代码的 html
  • 好的,我已经更新了我的问题

标签: javascript node.js dom cheerio


【解决方案1】:

试试这样:

let $ = cheerio.load(html)

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

console.log($.html())

【讨论】:

    【解决方案2】:

    根据个人上下文替换项目

    这演示了如何将不安全的 URL 替换为安全的 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://'))
    })
    

    【讨论】:

    猜你喜欢
    • 2021-10-18
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2019-08-05
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    相关资源
    最近更新 更多