如何更改标签名BeautifulSoup? [英] How to change tag name with BeautifulSoup?

查看:212
本文介绍了如何更改标签名BeautifulSoup?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python + BeautifulSoup解析HTML文档。

I am using python + BeautifulSoup to parse an HTML document.

现在我需要替换所有的< H2类=SomeClass的> 元素在HTML文档中,用< H1类= SomeClass的>

Now I need to replace all <h2 class="someclass"> elements in an HTML document, with <h1 class="someclass">.

如何更改标签名称,没有文件改变什么吗?

How can I change the tag name, without changing anything else in the document?

推荐答案

我不知道你是如何访问标记但对我来说了以下工作:

I don't know how you're accessing tag but the following works for me:

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('h2')
    h2.name = 'h1'
    print soup

打印输出汤命令是:

<html>
<h1 class='someclass'>some title</h1>
<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>

正如你所看到的, H2 成为 H1 。并没有别的文件中的变化。我使用Python 2.6和3.2.0 BeautifulSoup

As you can see, h2 became h1. And nothing else in the document changed. I am using Python 2.6 and BeautifulSoup 3.2.0.

如果您有多个 H2 更多你想改变它们,你可以简单的做:

If you have more than one h2 and you want to change them all, you could simple do:

soup = BeautifulSoup.BeautifulSoup(your_data)
while True: 
    h2 = soup.find('h2')
    if not h2:
        break
    h2.name = 'h1'

这篇关于如何更改标签名BeautifulSoup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆