论坛首页 编程语言技术论坛

html to text用正则表达式将HTML文件转换为TXT文件

浏览 4445 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-02-13  
import re
filename=raw_input('input a filename,please  ')
s=file(filename).read()
ss=s.replace('\n','')
ss=ss.replace(' ','')
ss=ss.replace('»','')
ss=re.sub("<!--.+?-->",' ',ss)
tem=re.sub("<.*?>",'',ss)
w=open('zip.txt','w')
w.write(tem)
w.close()

   发表时间:2009-02-18  
只能去掉标签吧,格式怎么办?&nbsp转换为文本空格
0 请登录后投票
   发表时间:2009-05-14  
也可以用BeautifulSoup来处理
soup = BeautifulSoup.BeautifulSoup(html)
text = soup.findAll(text=True)
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics