Insert HTML into an element with BeautifulSoup

﹥>﹥吖頭↗ 提交于 2020-05-13 12:17:01

问题


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">&lt;h3 id="feature_title"&gt;The Title &lt;/h3&gt;... &lt;div&gt;</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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!