Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using BeautifulSoup to insert an element before closing body

What is the most efficient way to insert an element as last one in the body of an HTML page?

like image 462
jldupont Avatar asked Dec 01 '10 00:12

jldupont


People also ask

How do you add a tag to a Beautiful Soup object?

A new tag can be created by calling BeautifulSoup's inbuilt function new_tag(). Inserting a new tag using the append() method : The new tag is appended to the end of the parent tag.

What function in Beautiful Soup will remove a tag from the HTML tree and destroy it?

decompose() removes a tag from the tree of a given HTML document, then completely destroys it and its contents.


1 Answers

See my answer to a previous question along the same lines:

  • Extract all <script> tags in an HTML page and append to the bottom of the document

The following should insert just fine:

soup.body.insert(len(soup.body.contents), yourelement)
like image 62
pyfunc Avatar answered Oct 10 '22 20:10

pyfunc