Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4065102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:56:26+00:00 2026-05-20T15:56:26+00:00

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

  • 0

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:56:27+00:00Added an answer on May 20, 2026 at 3:56 pm

    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
    

    Output of print soup command is:

    <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>
    

    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'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python and I'm using BeautifulSoup to parse a website and then
I am scraping HTML file using BeautifulSoup in python. I want to delete text
I'm using BeautifulSoup to extract some text from an HTML but I just can't
Using Python 2.6, is there a way to check if all the items of
I'm using python and I need to map locations like Bloomington, IN to GPS
I need to pull out all of the NodeGroup elements out of an XML
I'm trying to parse an RSS/Podcast feed using Beautifulsoup and everything is working nicely
I'm using python and beautifulsoup (new to both!), and I want to login to
I am using BeautifulSoup in Python and am having trouble replacing some tags. I
I'd like to extract the text from an HTML file using Python. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.