How to change tag name with BeautifulSoup?

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

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

Now I need to replace all

elements in an HTML document, with

.

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

回答1:

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

import BeautifulSoup  if __name__ == "__main__":     data = """  

some title

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  • Aliquam tincidunt mauris eu risus.
  • Vestibulum auctor dapibus neque.
""" soup = BeautifulSoup.BeautifulSoup(data) h2 = soup.find('h2') h2.name = 'h1' print soup

Output of print soup command is:

 

some title

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  • Aliquam tincidunt mauris eu risus.
  • Vestibulum auctor dapibus neque.

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.

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' 


回答2:

It's just:

tag.name = 'new_name' 


回答3:

From BeautifulSoup docs

from BeautifulSoup import BeautifulSoup, Tag soup = BeautifulSoup("

TEXTHERE

") tag = Tag(soup, "h1", [("class", "someclass")]) tag.insert(0, "TEXTHERE") soup.h2.replaceWith(tag) print soup #

TEXTHERE



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!