I would like to add this string for example: "{% some_tag dog cat %}"
after some HTML element with BeautifulSoap.
I've used something like: elem.insert_after(soup.new_string("{% some_tag dog cat %}"))
but this also added </link></meta>
tags to the object.
How this can be done properly?
Thanks
You can use the Python "html.parser" with BeautifulSoup that doesn't try to produce a valid document:
>>> soup = BeautifulSoup('<h1>header</h1>', 'html.parser')
>>> soup
<h1>header</h1>
>>> str = NavigableString("{% some_tag dog cat %}")
>>> soup.append(str)
>>> soup
<h1>header</h1>{% some_tag dog cat %}
see also https://www.crummy.com/software/BeautifulSoup/bs4/doc/#differences-between-parsers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.