【问题标题】:Python How to remove empty line in htmlPython如何删除html中的空行
【发布时间】:2016-12-21 13:29:09
【问题描述】:

我有一些问题。 我从 html 中删除了一些标签。但我希望输出没有空行。喜欢这个。

<!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>

我的预期输出是

<!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>

如何去除html中的空行?我可以用美丽的汤吗?或者任何图书馆?

更新

我尝试将我的代码与@elethan 的答案结合起来,但我遇到了一些错误

我的代码是

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

但我得到了这样的输出(抱歉不能粘贴 html) 实际上它一开始还不错,但几乎到了结尾 nul nul nul (就像屏幕截图一样)。

如何去除nul nul nul?

【问题讨论】:

  • 您应该在此处粘贴 HTML 而不是图片...
  • 您可以将文本作为文本而不是屏幕截图发布吗?
  • Sublime 是很棒的编辑器,但是请您将代码作为文本插入好吗?
  • 在您的 IDE 中,将 \n\n 替换为 \n

标签: python html beautifulsoup bs4


【解决方案1】:

在您的代码中,首先从文件中删除所有额外的换行符:

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

然后将过滤后的文本写回文件:

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

或者在一个 with 块中完成整个事情:

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

根据您现有的代码(您应该将其添加到您的问题中),您也许可以简单地将我的代码的过滤部分添加到您拥有的代码中。

【讨论】:

  • 嘿,我试过你的答案,但遇到了一些问题。我更新我的问题。请帮我。谢谢
【解决方案2】:

是的,您可以使用 Beautifulsoup,而且非常简单。

BS4 将尝试修复损坏的 html 标记,例如最后一行 &lt;/body&gt;&lt;/html&gt; 并删除空格。不同解析器的结果会略有不同,'lxml' 解析器表现良好。

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

出来:

<!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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2015-03-20
    • 2018-12-20
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    相关资源
    最近更新 更多