Ich habe ein Problem. Ich entferne einige Tags aus HTML. Aber ich möchte, dass die Ausgabe keine leere Zeile hat. Wie dieser.Python Wie leere Zeile in HTML zu entfernen
<!DOCTYPE html>
<html itemscope="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>
<body id="bodyart">
<div id="skinads" style="position:fixed;width:100%;">
<div class="main">
<div class="f1" style="height:600px;width:90px;left:-97px:position:relative;text-align:right;z-index:999999">
<div id="div-Left-Skin" style="width:90px; height:600px;display:none">
</div>
</div>
<div class="fr" style="height:600px;width;90px;right:-97px;position:relative;text-align:left;z-index:999999">
<div id="div-Right-Skin" style="width:90px; height:600px;display:none">
</div>
</div>
</div>
<div class="cl2"></div>
</div>
<div id="fb-root"></div>
Meine erwartete Ausgabe ist
<!DOCTYPE html>
<html itemscope="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>
<body id="bodyart">
<div id="skinads" style="position:fixed;width:100%;">
<div class="main">
<div class="f1" style="height:600px;width:90px;left:-97px:position:relative;text-align:right;z-index:999999">
<div id="div-Left-Skin" style="width:90px; height:600px;display:none">
</div>
</div>
<div class="fr" style="height:600px;width;90px;right:-97px;position:relative;text-align:left;z-index:999999">
<div id="div-Right-Skin" style="width:90px; height:600px;display:none">
</div>
</div>
</div>
<div class="cl2"></div>
</div>
<div id="fb-root"></div>
Wie leere Zeile in html entfernen? Kann ich beautifulsoup verwenden? Oder irgendeine Bibliothek?
UPDATE
ich versuche, meinen Code mit @elethan ‚s anwer zu kombinieren, aber ich habe einige Fehler
mein Code
from list import get_filepaths
from bs4 import BeautifulSoup
from bs4 import Comment
filenames = get_filepaths(r"C:\Coba")
index = 0
for f in filenames:
file_html=open(str(f),"r")
soup = BeautifulSoup(file_html,"html.parser")
[x.extract() for x in soup.find_all('script')]
[x.extract() for x in soup.find_all('style')]
[x.extract() for x in soup.find_all('meta')]
[x.extract() for x in soup.find_all('noscript')]
[x.extract() for x in soup.find_all(text=lambda text:isinstance(text, Comment))]
index += 1
stored_file = "PreProcessing\extracts" + '{0:03}'.format(index) + ".html"
filewrite = open(stored_file, "w")
filewrite.write(str(soup) + '\n')
with open(stored_file, 'r+') as f:
lines = [i for i in f.readlines() if i and i != '\n']
f.seek(0)
f.writelines(lines)
f.truncate()
filewrite.close
, aber ich habe die Ausgabe so (sorry kippe paste die html) eigentlich ist es ziemlich gut am Anfang, aber fast das Ende dort nul nul nul (wie der Bildschirmschoner).
wie die nul nul nul entfernen?
Sie sollten die HTML hier statt Bilder einfügen ... – blacksite
Könnten Sie Text als Text statt als Screenshots hinterlassen bitte? – khelwood
Sublime ist ein großartiger Editor, aber können Sie bitte Code als Text einfügen? –