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

  • SEARCH
  • Home
  • 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 6579729
In Process

The Archive Base Latest Questions

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

How can I remove certain attributes such as id, style, class, etc. from HTML

  • 0

How can I remove certain attributes such as id, style, class, etc. from HTML code?

I thought I could use the lxml.html.clean module, but as it turned out I can only remove style attributes with Clean(style=True).clean_html(code). I’d prefer not to use regular expressions for this task (attributes could change).

What I would like to have:

from lxml.html.clean import Cleaner

code = '<tr id="ctl00_Content_AdManagementPreview_DetailView_divNova" class="Extended" style="display: none;">'

cleaner = Cleaner(style=True, id=True, class=True)
cleaned = cleaner.clean_html(code)

print cleaned
'<tr>'

Thanks in advance!

  • 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-25T15:56:05+00:00Added an answer on May 25, 2026 at 3:56 pm

    cleaner.Cleaner.__call__ has a safe_attrs_only parameter. When set to True, only attributes in clean.defs.safe_attrs are preserved. You can remove any or all attributes by changing clean.defs.safe_attrs. Just be sure to change it back when you are done.

    import lxml.html.clean as clean
    
    code = '<tr id="ctl00_Content_AdManagementPreview_DetailView_divNova" class="Extended" style="display: none;">'
    
    safe_attrs = clean.defs.safe_attrs
    cleaner = clean.Cleaner(safe_attrs_only=True, safe_attrs=frozenset())
    cleansed = cleaner.clean_html(code)
    
    print(cleansed)
    

    yields

    <tr></tr>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I remove certain font size options from the font size selector in
I'm wondering how I can remove a certain class after a specific div is
How can I remove certain element inside html()? For instance, I have this html
How can I remove parts of a string up to a certain character? Ex.)
I am wondering how can I hide/remove a tag after a certain amount of
I want to remove all images from a HTML page (actually tinymce user input)
i have a regular expression to remove certain parts from a URI. However it
How can I remove certain items of an array? Let's say I have an
I'm unclear how to remove certain objects from Core Data database. I think I've
I have been trying to use a C# Regex unsuccessfully to remove certain strings

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.