Python Forum
Inserting line feeds and comments into a beautifulsoup string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inserting line feeds and comments into a beautifulsoup string
#1
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
using Ubuntu 18.04.4 LTS, Python 3.8
having substituted «https://www.lilo.org/fr/» to google, «https://protonmail.com/» to any other unsafe mail service and bépo to azerty (french keyboard layouts)
Reply
#2
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:~$ 
using Ubuntu 18.04.4 LTS, Python 3.8
having substituted «https://www.lilo.org/fr/» to google, «https://protonmail.com/» to any other unsafe mail service and bépo to azerty (french keyboard layouts)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 542 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,588 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  How do I add comments from a text-file to an array of folders? clausneergaard 2 1,799 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,388 Sep-27-2022, 01:38 PM
Last Post: buran
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 1,536 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,125 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Is it possible to write a python script to block twitter feeds? cubangt 0 879 Apr-07-2022, 04:14 PM
Last Post: cubangt
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,326 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  append a string to a modified line Mr_Blue 10 3,875 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
  How to capture string from a line to certain line jerald 1 1,920 Jun-30-2021, 05:13 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020