html转换成文本格式,Python将html转换为文本和模拟格式

我有一个更简单的任务代码:删除HTML标记,并在适当的位置插入换行符。也许这可以成为你的起点。

Python的textwrap模块可能有助于创建缩进的文本块。class HtmlTool(object):

"""

Algorithms to process HTML.

"""

#Regular expressions to recognize different parts of HTML.

#Internal style sheets or JavaScript

script_sheet = re.compile(r".*?(\1>)",

re.IGNORECASE | re.DOTALL)

#HTML comments - can contain ">"

comment = re.compile(r"", re.DOTALL)

#HTML tags:

tag = re.compile(r"<.>", re.DOTALL)

#Consecutive whitespace characters

nwhites = re.compile(r"[\s]+")

#

,

,
tags and associated closing tags

p_div = re.compile(r"?(p|div|br).*?>",

re.IGNORECASE | re.DOTALL)

#Consecutive whitespace, but no newlines

nspace = re.compile("[^\S\n]+", re.UNICODE)

#At least two consecutive newlines

n2ret = re.compile("\n\n+")

#A return followed by a space

retspace = re.compile("(\n )")

#For converting HTML entities to unicode

html_parser = HTMLParser.HTMLParser()

@staticmethod

def to_nice_text(html):

"""Remove all HTML tags, but produce a nicely formatted text."""

if html is None:

return u""

text = unicode(html)

text = HtmlTool.script_sheet.sub("", text)

text = HtmlTool.comment.sub("", text)

text = HtmlTool.nwhites.sub(" ", text)

text = HtmlTool.p_div.sub("\n", text) #convert

,

,
to "\n"

text = HtmlTool.tag.sub("", text) #remove all tags

text = HtmlTool.html_parser.unescape(text)

#Get whitespace right

text = HtmlTool.nspace.sub(" ", text)

text = HtmlTool.retspace.sub("\n", text)

text = HtmlTool.n2ret.sub("\n\n", text)

text = text.strip()

return text

代码中可能还有一些多余的regex。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值