批量html转text
但是发现MSHTML解析文件可能会出错,造成文本提取失败。 jigloo经过对10W+个html文件的测试,得出结论,htmlfile的容错比InternetExplorer.Application要差很多。 原文见:http://groups.google.com/group/python-cn/msg/c9221764bcafbc21 他的代码大致如下,IE使用稍烦: 对于我的简单任务,这就足够了。 有一个问题,如果有资源管理器打开着,运行这段代码会关闭资源管理器,并出错退出。比较奇怪,但应该不难解决,可能是IE控件的使用上还有问题。
self.__ie.Document.close()
File "C:/Python25/Lib/site-packages/win32com/client/dynamic.py", line 496, in
__getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Document.close
发布日期:2021-07-18 07:46:14
浏览次数:32
分类:技术文章
本文共 2342 字,大约阅读时间需要 7 分钟。
批量html转text (转载请注明来源于金庆的专栏) 原来的代码是参考“Recipe 12.11. Using MSHTML to Parse XML or HTML”,利用htmlfile提取文本。 将当前目录下的所有html文件转换为text文件。 def extractHtmlFile(htmlFilePath): ''' Extracthtmltextandsavetotextfile. ''' htmlData = file(htmlFilePath, ' r ' ).read() import win32com.clienthtml = win32com.client.Dispatch( ' htmlfile ' )html.writeln(htmlData)text = html.body.innerText.encode( ' gbk ' , ' ignore ' )...
# !/usr/bin/envpython import sys,os,re,codecs import time import win32com.client class htmlfile: def __init__ (self):self. __ie = win32com.client.Dispatch( ' InternetExplorer.Application ' )self. __ie .Silent = Trueself. __filename = '' self. __document = None def __del__ (self):self. __ie .Quit() def __getdocument (self,filename):filename = os.path.abspath(filename) if self. __filename != filename:self. __filename = filenameself. __ie .Navigate2(filename)self. __ie .Document.close() while self. __ie .Document.Body is None:time.sleep( 0.1 )self. __document = self. __ie .Document return self. __document def gettext(self,filename): return self. __getdocument (filename).Body.innerText def gettitle(self,filename): return self. __getdocument (filename).title def formattextpath(dir,htmlfile,htmltitle): ''' Formatthetextfilepathandreturn. ''' fname = htmltitle[: 6 ]fname = re.sub(r ' (/|/|:|*|?|<|>|||") ' , ' - ' ,fname)fname = fname + ' _ ' + os.path.splitext(htmlfile)[0] + ' .txt ' return os.path.join(root,fname) if __name__ == ' __main__ ' :hf = htmlfile() for root,dirs,names in os.walk(u ' . ' ): for name in names: if name.endswith( ' htm ' ) or name.endswith( ' html ' ):htmlpath = os.path.join(root,name)textpath = formattextpath(root,name,hf.gettitle(htmlpath)) print htmlpath, ' -> ' ,textpathfile(textpath, ' wb ' ).write(hf.gettext(htmlpath).encode( ' mbcs ' )) # Endofif. # Endofforname. # Endofforroot. del hf # Endofif.
self.__ie.Document.close()
File "C:/Python25/Lib/site-packages/win32com/client/dynamic.py", line 496, in
__getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Document.close
转载地址:https://blog.csdn.net/iteye_16215/article/details/81555712 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2025年01月08日 10时12分41秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java设计模式--中介者模式
2019-06-23
妈妈帮上云之路:云上平台架构与运维实践
2019-06-23
iOS开发中WiFi相关功能总结
2019-06-23
心理大师于际敬:技术人群心理特征及压力管理
2019-06-23
Android应用框架之Application&ActivityThread
2019-06-23
又来勒索,有完没完 - 数据库安全指南
2019-06-23
机器学习——随机森林算法及原理
2019-06-23
nginx配置详解-url重写、反向代理、负载均衡
2019-06-23
java线程间通信[实现不同线程之间的消息传递(通信),生产者和消费者模型]
2019-06-23
java继承
2019-06-23
对比MICROSOFT的SKYDRIVE测试免费同步方案和公司FTP方案
2019-06-23
操作系统随机密码,定时改密码
2019-06-23
【Swift】iOS开发历险记(二)
2019-06-23
利用shell脚本提高效率(切换用户不需要输入密码)
2019-06-23
IDEA 常用快捷键最佳实践
2019-06-23
实现类似于CISCO路由器的命令行帮助界面
2019-06-23
400 错误
2019-06-23