问题
When I try to insert the following HTML into an element
<div class="frontpageclass"><h3 id="feature_title">The Title</h3>... </div>
bs4 is replacing it like this:
<div class="frontpageclass"><h3 id="feature_title">The Title </h3>... <div></div>
I am using string and it is still messing up the format.
with open(html_frontpage) as fp:
soup = BeautifulSoup(fp,"html.parser")
found_data = soup.find(class_= 'front-page__feature-image')
found_data.string = databasedata
If I try to use found_data.string.replace_with I get a NoneType error. found_data is of type tag.
similar issue but they are using div, not class
回答1:
Setting the element .text or .string causes the value to be HTML-encoded, which is the right thing to do. It ensures that the text you insert will appear 1:1 when the document is displayed in a browser.
If you want to insert actual HTML, you need to insert new nodes into the tree.
from bs4 import BeautifulSoup
# always define a file encoding when working with text files
with open(html_frontpage, encoding='utf8') as fp:
soup = BeautifulSoup(fp, "html.parser")
target = soup.find(class_= 'front-page__feature-image')
# empty out the target element if needed
target.clear()
# create a temporary document from your HTML
content = '<div class="frontpageclass"><h3 id="feature_title">The Title</h3>...</div>'
temp = BeautifulSoup(content)
# the nodes we want to insert are children of the <body> in `temp`
nodes_to_insert = temp.find('body').children
# insert them, in source order
for i, node in enumerate(nodes_to_insert):
target.insert(i, node)
来源:https://stackoverflow.com/questions/52056240/insert-html-into-an-element-with-beautifulsoup