Skip to main content

The version on `github <http://github.com/jsocol/bleach>`_ is the most up-to-date and includes the latest bug fixes.

Bleach is an HTML sanitizing library designed to strip disallowed tags and attributes based on a whitelist, and can additionally autolinkify URLs in text with an extra filter layer that Django’s urlize filter doesn’t have.

Basic Use

The simplest way to use Bleach:

>>> from bleach import Bleach

>>> bl = Bleach()

>>> bl.clean('an <script>evil()</script> example')
'an &lt;script&gt;evil()&lt;/script&gt; example'

# to linkify URLs and email addresses, use
>>> bl.linkify('a http://example.com url')
'a <a href="http://example.com" rel="nofollow">http://example.com</a> url'

clean() also fixes up some common errors:

>>> from bleach import Bleach

>>> bl = Bleach()

>>> bl.clean('unbalanced <em>tag')
'unbalanced <em>tag</em>'

Advanced Use

Bleach is relatively configurable.

Clean - Advanced

clean() takes up to two optional arguments, tags and attributes, which are instructions on what tags and attributes to allow, respectively.

tags is a list of whitelisted tags:

>>> from bleach import Bleach

>>> bl = Bleach()

>>> TAGS = ['b', 'em', 'i', 'strong']

>>> bl.clean('<abbr>not allowed</abbr>', tags=TAGS)
'&lt;abbr&gt;not allowed&lt;/abbr&gt;'

attributes is either a list or, more powerfully, a dict of allowed attributes. If a list is used, it is applied to all allowed tags, but if a dict is use, the keys are tag names, and the values are lists of attributes allowed for that tag.

For example:

>>> from bleach import Bleach

>>> bl = Bleach()

>>> ATTRS = {'a': ['href']}

>>> bl.clean('<a href="/" title="fail">link</a>', attributes=ATTRS)
'<a href="/">link</a>'

Linkify - Advanced

If you pass nofollow=False to linkify(), links will not be created with rel="nofollow". By default, nofollow is True. If nofollow is True, then links found in the text will have their rel attributes set to nofollow as well, otherwise the attribute will not be modified.

Configuring linkify() is somewhat more complicated. linkify() passes data through different filters before returning the string. By default, these filters do nothing, but if you subclass Bleach, you can override them.

All the filters take and return a single string.

filter_url

filter_url(self, url) is applied to URLs before they are put into the href attribute of the link. If you need these links to go through a redirect or outbound script, filter_url() is the function to override.

For example:

import urllib

from bleach import Bleach

class MyBleach(Bleach):
    def filter_url(self, url):
        return 'http://example.com/bounce?u=%s' % urllib.quote(url)

Now, use MyBleach instead of Bleach and linkify() will route urls through your bouncer.

filter_text

This filter is applied to the link text of linkified URLs.

Metadata

Release files for bleach 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bleach 0.3.3
File Size Uploaded
bleach-0.3.3.tar.gz 7.7 kB Details

Release files / bleach-0.3.3.tar.gz

Download URL bleach-0.3.3.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
bf3e80e6d42fbc0581bea8032d14b6633be652094452a36593e7fb415aba6194
BLAKE2b-256 checksum
How to use checksums
ea301bff7f07eb4b59ed6b84fda817eef6c98c709de7519874ac2ce299535782
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

6.4.0

2 release files

6.3.0

2 release files

6.2.0

2 release files

6.1.0

2 release files

6.0.0

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.3

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.5

2 release files

3.1.4

2 release files

3.1.3

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.1.0

3 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1

2 release files

2.0.0

2 release files

1.5.0

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

1 release file

1.4

1 release file

1.2.2

1 release file

1.2.1

1 release file

1.2

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.3.4

1 release file

This release

0.3.3 This release

1 release file

0.3.1

2 release files

0.3

2 release files

0.2.2

2 release files

0.2.1

3 release files

0.2

0.1.2

0.1.1

0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page