From a4d113a2f6648d7f97d29bbbd2634949a4050eb0 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 20 May 2013 10:20:40 -0400 Subject: Gave new_string() the ability to create subclasses of NavigableString. [bug=1181986] --- doc/source/index.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'doc/source') 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 + # Hello there + 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()``:: -- cgit v1.2.3