• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python转换HTML到Text纯文本的方法

Python转换HTML到Text纯文本的方法

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了sublime text python,sublime text3 python,python text,python get text,python tkinter text等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Python转换HTML到Text纯文本的方法。分享给大家供大家参考。具体分析如下:

今天项目需要将HTML转换为纯文本,去网上搜了一下,发现Python果然是神通广大,无所不能,方法是五花八门。

拿今天亲自试的两个方法举例,以方便后人:

方法一:

1. 安装nltk,可以去pipy装

(注:需要依赖以下包:numpy, PyYAML)

2.测试代码:
>>> aa = r'''''
<html>
    <body>
 <b>Project:</b> DeHTML<br>
 <b>Description</b>:<br>
 This small script is intended to allow conversion from HTML markup to 
 plain text.
    </body>
</html>
'''
>>> aa 
'\n<html>\n            <body>\n                <b>Project:</b> DeHTML<br>\n                <b>Description</b>:<br>\n                This small script is intended to allow conversion from HTML markup to \n                plain text.\n            </body>\n        </html>\n        ' 
>>> <strong>print nltk.clean_html(aa)</strong> 
Project: DeHTML  
     Description :  
    This small script is intended to allow conversion from HTML markup to  
    plain text.</div>

方法二:

如果觉得nltk太笨重,大材小用的话,可以自己写代码,代码如下:
from re import sub 
from sys import stderr 
from traceback import print_exc 
 
class _DeHTMLParser(HTMLParser): 
    def __init__(self): 
        HTMLParser.__init__(self) 
        self.__text = [] 
 
    def handle_data(self, data): 
        text = data.strip() 
        if len(text) > 0: 
            text = sub('[ \t\r\n]+', ' ', text) 
            self.__text.append(text + ' ') 
 
    def handle_starttag(self, tag, attrs): 
        if tag == 'p': 
            self.__text.append('\n\n') 
        elif tag == 'br': 
            self.__text.append('\n') 
 
    def handle_startendtag(self, tag, attrs): 
        if tag == 'br': 
            self.__text.append('\n\n') 
 
    def text(self): 
        return ''.join(self.__text).strip() 
 
 
def dehtml(text): 
    try: 
        parser = _DeHTMLParser() 
        parser.feed(text) 
        parser.close() 
        return parser.text() 
    except: 
        print_exc(file=stderr) 
        return text 
 
 
def main(): 
    text = r'''''
        <html>
            <body>
                <b>Project:</b> DeHTML<br>
                <b>Description</b>:<br>
                This small script is intended to allow conversion from HTML markup to 
                plain text.
            </body>
        </html>
    ''' 
    print(dehtml(text)) 
 
 
if __name__ == '__main__': 
    main()</div>

运行结果:

>>> ================================ RESTART ================================ 
>>>  
Project: DeHTML  
Description :  
This small script is intended to allow conversion from HTML markup to plain text. 

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python转换HTML到Text纯文本的方法
  • python实现sublime3的less编译插件示例
  • python实现sublime3的less编译插件示例

相关文章

  • python使用socket连接远程服务器的方法
  • Python魔术方法详解
  • python网络编程学习笔记(四):域名系统
  • python爬虫scrapy运行ImportError: No module named win32api错误
  • Python 递归函数详解及实例
  • Flask框架的学习指南之开发环境搭建
  • python中as用法实例分析
  • python 不关闭控制台的实现方法
  • python实现文本去重且不打乱原本顺序
  • Python随机数用法实例详解【基于random模块】

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 利用numpy+matplotlib绘图的基本操作教程
    • python计算圆周率pi的方法
    • Python字符串转换成浮点数函数分享
    • 用Python编写一个基于终端的实现翻译的脚本
    • python重试装饰器示例
    • Python安装使用命令行交互模块pexpect的基础教程
    • python实现在字符串中查找子字符串的方法
    • python使用win32com库播放mp3文件的方法
    • python通过pil模块将raw图片转换成png图片的方法
    • Python2.x利用commands模块执行Linux shell命令

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有