Skip to content Skip to sidebar Skip to footer

Python How To Remove Empty Line In Html

I have some problem. I remove some tag from html. But I want the output don't have empty line. Like this one. ]

Then write the filtered text back to the file:

withopen(my_html_file, 'w') as f:
    f.writelines(lines)

Or to do the whole thing in a single with block:

withopen(my_html_file, 'r+') as f:
    lines = [i for i in f.readlines() if i and i != '\n']
    f.seek(0)
    f.writelines(lines)
    f.truncate()

Depending on your existing code (which you should add to your question), you might be able to simply add the filtering part of my code to what you have.

Solution 2:

Yes, you can use Beautifulsoup, and it's very simple.

BS4 will try to fix the broken html tag, like the last line </body></html> and remove the white space. The results of different parser will be slightly different, and the 'lxml' parser performs well.

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'lxml')
print(str(soup))

out:

<!DOCTYPE html><htmlitemscope="itemscope"itemtype="http://schema.org/WebPage"lang="id-ID"><head><title>Kenya Kasat Narkoba Polres Bintan Diganti? Ini Pesan Kapolres melada Kasatreskrim Baru - Tribun Batam</title></head><bodyid="bodyart"><divid="skinads"style="position:fixed;width:100%;"><divclass="main"><divclass="f1"style="height:600px;width:90px;left:-97px:position:relative;text-align:right;z-index:999999"><divid="div-Left-Skin"style="width:90px; height:600px;display:none"></div></div><divclass="fr"style="height:600px;width;90px;right:-97px;position:relative;text-align:left;z-index:999999"><divid="div-Right-Skin"style="width:90px; height:600px;display:none"></div></div></div><divclass="cl2"></div></div><divid="fb-root"></div></body></html>

Post a Comment for "Python How To Remove Empty Line In Html"