python实现的简单html文本转纯text
生活随笔
收集整理的這篇文章主要介紹了
python实现的简单html文本转纯text
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一個python實現(xiàn)的簡單轉(zhuǎn)html轉(zhuǎn)純text的函數(shù)
?
import re from html import unescapedef html_to_plain_text(html):text = re.sub('<head.*?>.*?</head>', '', html, flags=re.M | re.S | re.I)text = re.sub('<a\s.*?>', ' HYPERLINK ', text, flags=re.M | re.S | re.I)text = re.sub('<.*?>', '', text, flags=re.M | re.S)text = re.sub(r'(\s*\n)+', '\n', text, flags=re.M | re.S)return unescape(text)例如有一段html文本
<HTML><HEAD><TITLE></TITLE><META http-equiv="Content-Type" content="text/html; charset=windows-1252"><STYLE>A:link {TEX-DECORATION: none}A:active {TEXT-DECORATION: none}A:visited {TEXT-DECORATION: none}A:hover {COLOR: #0033ff; TEXT-DECORATION: underline}</STYLE><META content="MSHTML 6.00.2713.1100" name="GENERATOR"></HEAD> <BODY text="#000000" vLink="#0033ff" link="#0033ff" bgColor="#CCCC99"><TABLE borderColor="#660000" cellSpacing="0" cellPadding="0" border="0" width="100%"><TR><TD bgColor="#CCCC99" valign="top" colspan="2" height="27"> <font size="6" face="Arial, Helvetica, sans-serif" color="#660000"> <b>OTC</b></font></TD></TR><TR><TD height="2" bgcolor="#6a694f"> <font size="5" face="Times New Roman, Times, serif" color="#FFFFFF"> <b> Newsletter</b></font></TD><TD height="2" bgcolor="#6a694f"><div align="right"><font color="#FFFFFF"> <b>Discover Tomorrow's Winners </b></font></div></TD></TR><TR><TD height="25" colspan="2" bgcolor="#CCCC99"><table width="100%" border="0" ...轉(zhuǎn)換以后的結果
OTCNewsletter Discover Tomorrow's Winners For Immediate Release Cal-Bay (Stock Symbol: CBYI) Watch for analyst "Strong Buy Recommendations" and several advisory newsletters picking CBYI. CBYI has filed to be traded on the OTCBB, share prices historically INCREASE when companies get listed on this larger trading exchange. CBYI is trading around 25 cents and should skyrocket to $2.66 - $3.25 a share in the near future. Put CBYI on your watch list, acquire a position TODAY. REASONS TO INVEST IN CBYI A profitable company and is on track to beat ALL earnings estimates! One of the FASTEST growing distributors in environmental & safety equipment instruments. Excellent management team, several EXCLUSIVE contracts. IMPRESSIVE client list including the U.S. Air Force, Anheuser-Busch, Chevron Refining and Mitsubishi Heavy Industries, GE-Energy & Environmental Research. RAPIDLY GROWING INDUSTRY Industry revenues exceed $900 million, estimates indicate that there could be as much as $25 billi ...一個將html格式的email轉(zhuǎn)換為純text的例子:
def email_to_text(email):html = Nonefor part in email.walk():ctype = part.get_content_type()if not ctype in ("text/plain", "text/html"):continuetry:content = part.get_content()except: # in case of encoding issuescontent = str(part.get_payload())if ctype == "text/plain":return contentelse:html = contentif html:return html_to_plain_text(html)?
總結
以上是生活随笔為你收集整理的python实现的简单html文本转纯text的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数字电路设计中亚稳态与MTBF
- 下一篇: python 爬虫 教务系统模拟登陆 并