Python Forum

Full Version: Inserting line feeds and comments into a beautifulsoup string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

In a python script which creates html documents using beautifulsoup, I want to insert new lines and html comments between some tags, at specific spots inside the beautiful string, not inside the resulting html document.

As an example, starting from such a string :

<!DOCTYPE html>
<html><head><title>My html document</title></head><body><section id="register"/><section id="words"/></body></html>

I want to get this one :

Output:
<!DOCTYPE html> <html><head><title>My html document</title></head><body> <!-----------------here is the register section --------------> <section id="register"/> <!-----------------here is the words section --------------> <section id="words"/> <!----------------- no additionnal sections --------------> </body></html>
I have not been able to find out how to do.

With code as this
from bs4 import BeautifulSoup as btfs
from bs4 import Comment as cmt
nl=cmt("""
""")
marque='----------'
#spot=locating where to insert the comment
entête=btfs('', 'html.parser')
entête.append(cmt(nl))
entête.append(cmt(' '.join((marque,polytonique,atonique,marque))))
entête.append(cmt(nl))
spot.insert_before(entête)
I have been able to get :

Output:
<section id="mots"><!-- --><!------------ ἀγαθός [αγαθος] ------------><!-- --><article id="ἀγαθός">
when I'm looking for:

Output:
<section id="mots"> <!------------ ἀγαθός [αγαθος] ------------> <article id="ἀγαθός">
and also, in other situations, to

Output:
<section id="mots"> <article id="ἀγαθός">
Your input will be appreciated.

Arbiel
Hi

I actually found how to do : create a string with line feeds and comments and then converting it with beautifulsoup

arbiel@arbiel-NK3S-8-S4:~$ python
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup as btfs
>>> str="\r<!--- a comment --->\n"
>>> tag=btfs(str, 'html.parser')
>>> print(tag)
 <!--- a comment --->

>>> exit()
arbiel@arbiel-NK3S-8-S4:~$