How to remove a column from a table in beautifulsoup (Python)

I have an html table and I would like to delete a column. What is the easiest way to do this using BeautifulSoup or any other python library?

+4
source share
1 answer

lxml.html is better for managing HTML, IMO. Here is the code that will remove the second column of the HTML table.

from lxml import html text = """ <table> <tr><th>head 1</th><th>head 2</th><th>head 3</th></tr> <tr><td>item 1</td><td>item 2</td><td>item 3</td></tr> </table> """ table = html.fragment_fromstring(text) # remove middle column for row in table.iterchildren(): row.remove(row.getchildren()[1]) print html.tostring(table, pretty_print=True) 

Result:

 <table> <tr> <th>head 1</th> <th>head 3</th> </tr> <tr> <td>item 1</td> <td>item 3</td> </tr> </table> 
+2
source

Source: https://habr.com/ru/post/1303073/


All Articles