Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a new tag into a BeautifulSoup object?

Trying to get my head around html construction with BS.

I'm trying to insert a new tag:

self.new_soup.body.insert(3, """<div id="file_history"></div>""")    

when I check the result, I get:

&lt;div id="file_histor"y&gt;&lt;/div&gt; 

So I'm inserting a string that being sanitised for websafe html..

What I expect to see is:

<div id="file_history"></div> 

How do I insert a new div tag in position 3 with the id file_history?

like image 400
Jay Gattuso Avatar asked Jan 25 '14 20:01

Jay Gattuso


People also ask

How do you add a tag in Python?

Step-by-step Approach: We assign a new element in the tag object using new_tag(). We assign a string to the tag object to attach our tags to before or after it(as specified). We insert the tag before the string using insert_before() function.

Is tag an object of BeautifulSoup?

A tag object in BeautifulSoup corresponds to an HTML or XML tag in the actual page or document. Tags contain lot of attributes and methods and two important features of a tag are its name and attributes.

What is a tag in BeautifulSoup?

Going down. One of the important pieces of element in any piece of HTML document are tags, which may contain other tags/strings (tag's children). Beautiful Soup provides different ways to navigate and iterate over's tag's children.

How do you edit HTML with BeautifulSoup?

Step 1: First, import the libraries Beautiful Soup, os and re. Step 2: Now, remove the last segment of the path. Step 3: Then, open the HTML file in which you wish to make a change. Step 4: Moreover, parse the HTML file in Beautiful Soup.


2 Answers

See the documentation on how to append a tag:

soup = BeautifulSoup("<b></b>") original_tag = soup.b  new_tag = soup.new_tag("a", href="http://www.example.com") original_tag.append(new_tag) original_tag # <b><a href="http://www.example.com"></a></b>  new_tag.string = "Link text." original_tag # <b><a href="http://www.example.com">Link text.</a></b> 
like image 192
Guy Gavriely Avatar answered Sep 21 '22 21:09

Guy Gavriely


Use the factory method to create new elements:

new_tag = self.new_soup.new_tag('div', id='file_history') 

and insert it:

self.new_soup.body.insert(3, new_tag) 
like image 43
Birei Avatar answered Sep 20 '22 21:09

Birei