用 BeautifulSoup 替换 html 标签
我目前正在使用 BeautifulSoup 重新格式化一些 HTML 页面,但遇到了一些问题。
我的问题是原始 HTML 有这样的内容:
<li><p>stff</p></li>
以及
<li><div><p>Stuff</p></div></li>
使用
<li><div><p><strong>stff</strong></p></div><li>
BeautifulSoup 我希望消除 div 和 p 标签(如果存在),但保留 Strong 标签。
我正在浏览漂亮的汤文档,但找不到任何内容。 有想法吗?
谢谢。
I'm currently reformatting some HTML pages with BeautifulSoup, and I ran into bit of a problem.
My problem is that the original HTML has things like this:
<li><p>stff</p></li>
and
<li><div><p>Stuff</p></div></li>
as well as
<li><div><p><strong>stff</strong></p></div><li>
With BeautifulSoup I hope to eliminate the div and the p tags, if they exists, but keep the strong tag.
I'm looking through the beautiful soup documentation and couldn't find any.
Ideas?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个问题可能涉及旧版本的 BeautifulSoup 因为使用 bs4 你可以简单地使用 展开 函数:
This question probably refered to an older version of BeautifulSoup because with bs4 you can simply use the unwrap function:
您想要做的事情可以使用
replaceWith
来完成。您必须复制要用作替换的元素,然后将其作为参数提供给replaceWith
。replaceWith
的文档 非常清楚如何做到这一点。What you want to do can be done using
replaceWith
. You have to duplicate the element you want to use as the replacement, and then feed that as the argument toreplaceWith
. The documentation forreplaceWith
is pretty clear on how to do this.我看到了这个简单问题的很多答案,我也来这里看看一些有用的东西,但不幸的是我没有得到我正在寻找的东西,然后经过几次尝试我找到了这个答案的简单解决方案,这里是
所有 h2 标签转换为h1。您只需更改名称即可转换任何标签。
I saw many answers for this simple question, i also came here to see something useful but unfortunately i didn't get what i was looking for then after few tries I found a simple solution for this answer and here it is
All h2 tags converted to h1. You can convert any tag by just changing the name.
您可以编写自己的函数来剥离标签:
You can write your own function to strip tags:
简单的解决方案让整个节点意味着
div
:
替换为所需的标签/字符串。通过传递给 beautifulsoup 将转换后的字符串转换为可解析的字符串
我为
mint做了什么
示例:
输出:
<前><代码>-2^31 到 2^31-1
如果不进行操作,它会像这样(-231 到 231-1)
Simple solution get your whole node means
div
:<tag>
with required tag/string.Convert the converted string to parsable string by passing to beautifulsoup
What I have done for
mint
Example:
Output: