本文實(shí)例講述了Python轉(zhuǎn)換HTML到Text純文本的方法。分享給大家供大家參考。具體分析如下:
今天項(xiàng)目需要將HTML轉(zhuǎn)換為純文本,去網(wǎng)上搜了一下,發(fā)現(xiàn)Python果然是神通廣大,無所不能,方法是五花八門。
拿今天親自試的兩個(gè)方法舉例,以方便后人:
方法一:
1. 安裝nltk,可以去pipy裝
(注:需要依賴以下包:numpy, PyYAML)
2.測試代碼:
代碼如下:>>> import nltk
>>> 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.
方法二:
如果覺得nltk太笨重,大材小用的話,可以自己寫代碼,代碼如下:
代碼如下:from HTMLParser import HTMLParser
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 + ' ')
新聞熱點(diǎn)
疑難解答
圖片精選