summaryrefslogtreecommitdiff
path: root/doc/source
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2013-05-20 10:20:40 -0400
committerLeonard Richardson <leonardr@segfault.org>2013-05-20 10:20:40 -0400
commita4d113a2f6648d7f97d29bbbd2634949a4050eb0 (patch)
treed74845850aa94bb8899ae175fe32a6ec08321e1a /doc/source
parent9f370bad91d80570a57156f53c6a9efc918ff90f (diff)
Gave new_string() the ability to create subclasses of
NavigableString. [bug=1181986]
Diffstat (limited to 'doc/source')
-rw-r--r--doc/source/index.rst14
1 files changed, 14 insertions, 0 deletions
diff --git a/doc/source/index.rst b/doc/source/index.rst
index e254855..a91854c 100644
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -1806,6 +1806,20 @@ Python string in to ``append()``, or you can call the factory method
tag.contents
# [u'Hello', u' there']
+If you want to create a comment or some other subclass of
+``NavigableString``, pass that class as the second argument to
+``new_string()``::
+
+ from bs4 import Comment
+ new_comment = soup.new_string("Nice to see you.", Comment)
+ tag.append(new_comment)
+ tag
+ # <b>Hello there<!--Nice to see you.--></b>
+ tag.contents
+ # [u'Hello', u' there', u'Nice to see you.']
+
+(This is a new feature in Beautiful Soup 4.2.1.)
+
What if you need to create a whole new tag? The best solution is to
call the factory method ``BeautifulSoup.new_tag()``::