From 930adda6bf6e34e8ebcc446507f5227705f0be21 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 31 Dec 2018 11:53:03 -0500 Subject: Documentation update for extend(), insert_before(), and insert_after(). --- doc/source/index.rst | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/doc/source/index.rst b/doc/source/index.rst index 9bf9cf1..987b73d 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -1870,6 +1870,21 @@ like calling ``.append()`` on a Python list:: soup.a.contents # [u'Foo', u'Bar'] +``extend()`` +------------ + +Starting in Beautiful Soup 4.7.0, ``Tag`` also supports a method +called ``.extend()``, which works just like calling ``.extend()`` on a +Python list:: + + soup = BeautifulSoup("Soup") + soup.a.extend(["'s", " ", "on"]) + + soup + # Soup's on + soup.a.contents + # [u'Soup', u''s', u' ', u'on'] + ``NavigableString()`` and ``.new_tag()`` ------------------------------------------------- @@ -1938,7 +1953,7 @@ say. It works just like ``.insert()`` on a Python list:: ``insert_before()`` and ``insert_after()`` ------------------------------------------ -The ``insert_before()`` method inserts a tag or string immediately +The ``insert_before()`` method inserts tags or strings immediately before something else in the parse tree:: soup = BeautifulSoup("stop") @@ -1948,14 +1963,16 @@ before something else in the parse tree:: soup.b # Don'tstop -The ``insert_after()`` method moves a tag or string so that it -immediately follows something else in the parse tree:: +The ``insert_after()`` method inserts tags or strings immediately +following something else in the parse tree:: - soup.b.i.insert_after(soup.new_string(" ever ")) + div = soup.new_tag('div') + div.string = 'ever' + soup.b.i.insert_after(" you ", div) soup.b - # Don't ever stop + # Don't you
ever
stop
soup.b.contents - # [Don't, u' ever ', u'stop'] + # [Don't, u' you',
ever
, u'stop'] ``clear()`` ----------- @@ -2085,7 +2102,8 @@ Pretty-printing --------------- The ``prettify()`` method will turn a Beautiful Soup parse tree into a -nicely formatted Unicode string, with each HTML/XML tag on its own line:: +nicely formatted Unicode string, with a separate line for each +tag and each string: markup = 'I linked to example.com' soup = BeautifulSoup(markup) -- cgit v1.2.1