2016-12-21 5 views
-1

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? enter image description here

+1

Sie sollten die HTML hier statt Bilder einfügen ... – blacksite

+1

Könnten Sie Text als Text statt als Screenshots hinterlassen bitte? – khelwood

+0

Sublime ist ein großartiger Editor, aber können Sie bitte Code als Text einfügen? –

Antwort

0

In Ihrem Code, entfernen Sie zuerst alle zusätzlichen Zeilenumbrüche aus der Datei:

with open(my_html_file) as f: 
    lines = [i for i in f.readlines() if i and i != '\n'] 

Dann das gefilterte Text in die Datei schreiben zurück:

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

Oder die ganze Sache zu tun in einem einzigen with Block:

with open(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() 

Abhängig von Ihrem vorhandenen Code (den Sie Ihrer Frage hinzufügen sollten) können Sie den Filterteil meines Codes einfach zu dem hinzufügen, was Sie haben.

+0

hey, ich habe versucht, Ihre Antwort, aber ein Problem zu bekommen. Ich aktualisiere meine Frage. Bitte hilf mir. Vielen Dank –

0

Ja, Sie können Beautifulsoup verwenden, und es ist sehr einfach.

BS4 wird versuchen, das beschädigte HTML-Tag zu beheben, wie die letzte Zeile </body></html> und den Leerraum entfernen. Die Ergebnisse verschiedener Parser unterscheiden sich etwas, und der Parser "lxml" funktioniert gut.

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

aus:

<!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> 
</body></html>