在python中的html中删除标签之间的空格和换行符(aka取消格式化) [英] Removing spaces and newlines between tags in html (aka unformatting) in python

查看:572
本文介绍了在python中的html中删除标签之间的空格和换行符(aka取消格式化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个例子:

<p> Hello</p>
<div>hgello</div>
<pre>
   code
    code
<pre>

上交类似的内容

<p> Hello</p><div>hgello</div><pre>
    code
     code
<pre>

如何在python中执行此操作?我还大量使用< pre>标记,因此不能将所有'\ n'替换为''.

How to do this in python? I make also intensive use of < pre> tags so substituting all '\n' with '' is not an option.

做到这一点的最佳方法是什么?

What's the best way to do that?

推荐答案

您可以使用re.sub(">\s*<","><","[here your html string]").

也许是string.replace(">\n",">"),即寻找一个括起来的括号和一个换行符,然后删除换行符.

Maybe string.replace(">\n",">"), i.e. look for an enclosing bracket and a newline and remove the newline.

这篇关于在python中的html中删除标签之间的空格和换行符(aka取消格式化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆