remove trailing / from col tag

4 views
Skip to first unread message

Steve Clarke

unread,
Jul 2, 2024, 11:37:41 AM (17 hours ago) Jul 2
to beautifulsoup
Hey...

Anyone know how to make bs4 remove the training / from a col tag?

Here is a snippet of code that adds a colgroup to the first table in a html doc

def add_colgroup(html: str):

    soup = bs4.BeautifulSoup(html, 'html5lib')

 

    table = soup.table

    headers = table.find_all('th')

    colgroup = soup.new_tag('colgroup')

 

    for _ in headers:

        col = soup.new_tag('col')

        col.encode(formatter='html5')

        col['style'] = 'text-align: left;width: auto'

        colgroup.insert(1, col)

 

    table.insert(1, colgroup)

 

    return soup.prettify()



It creates the following;

<colgroup>

  <col style="text-align: left;width: auto"/>

  <col style="text-align: left;width: auto"/>

  <col style="text-align: left;width: auto"/>

  <col style="text-align: left;width: auto"/>

  <col style="text-align: left;width: auto"/>

</colgroup>



How do I remove the trailing / ?

Reply all
Reply to author
Forward
0 new messages