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 7248495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:08:45+00:00 2026-05-28T22:08:45+00:00

I want to strip all html tags from a string except some I specify.

  • 0

I want to strip all html tags from a string except some I specify.
If I call the constructor with default values everything works fine:

>>> cleaner = lxml.html.clean.Cleaner()
>>> cleaner.clean_html('''<i>italic</i><script>alert('');</script>''')
'<span><i>italic</i></span>'

But when I try to specify some tags, things doesn’t work anymore:

>>> allowed_tags = ['i','s']
>>> cleaner = lxml.html.clean.Cleaner(remove_unknown_tags=False,allow_tags=allowed_tags)
>>> cleaner.clean_html('''<i>italic</i><s>strike</s>''')
'<span></span>'

So what am i doing wrong?

  • 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-28T22:08:46+00:00Added an answer on May 28, 2026 at 10:08 pm

    As a workaround, you can add span and div tags to allowed_tags.

    UPD

    lxml.html.Cleaner tries to convert string to html tree by calling fromstring, which checks if document have some root node, and adds it if necessary. So you need to allow span and div tags

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to strip all the html tags from a string except ( <img>
I have to strip all HTML tags and attributes from a user input except
I want to strip tags from a html, but preserves it's line breaks. I
I want to remove all tags except the img tag, $words = strip_tags($words, '<img>');
I want to strip script tags out of this HTML at Pastebin: http://pastebin.com/mdxygM0a I
Is there a way to strip all HTML from XML with XSLT, like PHP
I used this code to strip all tags but I wan't to save some
I want to be able to strip off a header from a wav file.
I want to modify an internal webpage to strip away some of the onclick
I am trying to parse some HTML snippets and want to clean them up

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.