如何用a< b>在特定标签中包装初始字母??

发布于 2025-02-01 23:01:54 字数 629 浏览 4 评论 0 原文

我正在尝试使用python使用 Beautifulsoup 模块来执行以下操作:

在html的Div中,对于每个段落标签,我想在段落中的每个单词的第一个字母中添加一个粗体标签。例如:

<div class="body">
    <p>The quick brown fox</p>
</div>

哪个会读:快速的棕色狐狸

会变成

<div class="body">
    <p><b>T</b>he <b>q</b>uick <b>b</b>rown <b>f</b>ox</p>
</div>

t he q uick b row f < /strong>使用 bs4 ox

我一直找不到一个很好的解决方案来做到这一点,并且对想法开放。

I am trying to use the BeautifulSoup module with Python to do the following:

Within a div for HTML, for each paragraph tag, I want to add a bold tag to the first letter of each word within the paragraph. For example:

<div class="body">
    <p>The quick brown fox</p>
</div>

which would read: The quick brown fox

would then become

<div class="body">
    <p><b>T</b>he <b>q</b>uick <b>b</b>rown <b>f</b>ox</p>
</div>

that would read: The quick brown fox

Using bs4 i've been unable to find a good solution to do this and am open to ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2025-02-08 23:01:54

您可以使用 replace_with() list Gracemension - 提取 text / String String 从 Tag> Tag / bs4对象,将其作为文本处理,然后在替换标签上用新的 bs4对象< / code>:

soup.p.replace_with(
    BeautifulSoup(
        ' '.join([s.replace(s[0],f'<b>{s[0]}</b>') for s in soup.p.string.split(' ')]),'html.parser'
    )
)

示例

from bs4 import BeautifulSoup
html = '''
<div class="body">
    <p>The quick brown fox</p>
</div>'''
soup = BeautifulSoup(html,'html.parser')

soup.p.replace_with(
    BeautifulSoup(
        ' '.join([s.replace(s[0],f'<b>{s[0]}</b>') for s in soup.p.string.split(' ')]),'html.parser'
    )
)

soup
输出
<div class="body">
<b>T</b>he <b>q</b>uick <b>b</b>rown <b>f</b>ox
</div>

You could use replace_with() combined with list comprehension - Extract text / string from tag / bs4 object, process it as text and later on replace the tag with new bs4 object:

soup.p.replace_with(
    BeautifulSoup(
        ' '.join([s.replace(s[0],f'<b>{s[0]}</b>') for s in soup.p.string.split(' ')]),'html.parser'
    )
)

Example

from bs4 import BeautifulSoup
html = '''
<div class="body">
    <p>The quick brown fox</p>
</div>'''
soup = BeautifulSoup(html,'html.parser')

soup.p.replace_with(
    BeautifulSoup(
        ' '.join([s.replace(s[0],f'<b>{s[0]}</b>') for s in soup.p.string.split(' ')]),'html.parser'
    )
)

soup
Output
<div class="body">
<b>T</b>he <b>q</b>uick <b>b</b>rown <b>f</b>ox
</div>
桃酥萝莉 2025-02-08 23:01:54

我对Python如何详细解析HTML了解不多,但是我可以为您提供一些想法。

要查找&lt; p&gt; 标签,您可以使用Regex &lt; p。 (“&lt; p&gt;”)然后步行直到&lt;/p&gt;

为了添加&lt; b&gt; 标签,也许此代码可行:

def add_bold(s: str) -> str:
    ret = ""
    isFirstLet = True
    for i in s:
        if isFirstLet:
            ret += "<b>" + i + "</b>"
            isFirstLet = False
        else:
            ret += i
        if i == " ": isFirstLet = True
    return ret

I don't know much about how Python parses HTML in detail, but I can provide you with some ideas.

To find <p> tags, you can use RegEx <p.*?>.*?</p> or use str.find("<p>") and walk until </p>.

To add <b> tags, perhaps this code will work:

def add_bold(s: str) -> str:
    ret = ""
    isFirstLet = True
    for i in s:
        if isFirstLet:
            ret += "<b>" + i + "</b>"
            isFirstLet = False
        else:
            ret += i
        if i == " ": isFirstLet = True
    return ret
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文