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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener")
original_tag.append(new_tag)
original_tag
# <b><a href="http://www.example.com" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"></a></b>

new_tag.string = "Link text."
original_tag
# <b><a href="http://www.example.com" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">Link text.</a></b>

Method 2

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)

Method 3

Other answers are straight off from the documentation. Here is the shortcut:

from bs4 import BeautifulSoup

temp_soup = BeautifulSoup('<div id="file_history"></div>')
# BeautifulSoup automatically add <html> and <body> tags
# There is only one 'div' tag, so it's the only member in the 'contents' list
div_tag = temp_soup.html.body.contents[0]
# Or more simply
div_tag = temp_soup.html.body.div
your_new_soup.body.insert(3, div_tag)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x