将<strong>标签替换为h2标签

2024-04-18 04:59:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一些漂亮的组合代码,它将把被标记包围的每一段文本,并将标记更改为标记-但是,只有当它只是一行没有其他写入/输出文本的情况下。在

这可能吗?在

Before

为了这个

After

但这将保持不变:

Unchanged

我知道下面的方法可以转换所有的强项。我怎么能只得到重要的?在

import BeautifulSoup

if __name__ == "__main__":
    data = """
<html>
<h2 class='someclass'>some title</h2>
<ul>
   <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
   <li>Aliquam tincidunt mauris eu risus.</li>
   <li>Vestibulum auctor dapibus neque.</li>
</ul>
</html>

    """
    soup = BeautifulSoup.BeautifulSoup(data)
    h2 = soup.find('strong')
    h2.name = 'h1'
    print soup

Tags: 方法代码name标记文本importdatahtml
2条回答

您可以找到所有strong元素并检查^{}的长度:

from bs4 import BeautifulSoup

data = """
<html>
<p><strong>Like this</strong></p>
<p>Hello, <strong>world</strong>
</html>
"""

soup = BeautifulSoup(data)
for strong in soup.find_all('strong'):
    if len(strong.parent) == 1:
        strong.name = 'h1'
print soup

打印(请参阅第一个strong标记已被替换,第二个标记未被替换):

^{pr2}$

或者,用更简洁的形式:

for strong in soup.find_all('strong', lambda x: x and len(x.parent) == 1):
    strong.name = 'h1'

作为补充说明,您使用的是^{},它不再被维护;请考虑升级到^{}

pip install beautifulsoup4

呃。。。这可能没有那么有效,但写起来肯定更简单:

data = data.replace('<p><strong>', '<p><h2>')
data = data.replace('</strong></p>', '</h2></p>')

还是我误解了一些基本的东西结构更换()? 在

虽然这不是很复杂,但是如果html是一致的,那么就可以了

编辑:使用正则表达式的更复杂的解决方案:

^{pr2}$

相关问题 更多 >