python - 关于代码的优化问题
问题描述
我新手写的代码,用来处理爬虫下来的htm文件内容,虽然解决问题,但是会有遗漏文件不处理。爬虫是爬一些文章的网站下来的,和网页另存为没什么区别。
想大神们帮我看看我的代码,怎么优化不会有遗漏。比较小白的代码,麻烦了!!!
# -*- coding: utf-8 -*import reimport globfilename_list = glob.glob(’*.html’)for i in filename_list: txt = '' with open(i, 'r') as htmfile:txt = htmfile.read() scdy = r'<hr[sS]*?<hr' onedotxt = re.findall(scdy, txt) if onedotxt:r = onedotxt[0] twotxt=re.sub(’<[^>]*>’, ’’, r) threetxt=re.sub(’<hr’, ’’, twotxt) fourtxt=re.sub(’’’, ’’, threetxt) fivetxt=re.sub(’”’, ’'’, fourtxt) sixtxt=re.sub(’“’, ’'’, fivetxt)endstr=re.sub(’–’, ’-’, sixtxt) name = endstr.split(’n’)[1] with open(name+'.txt', 'w') as wf: wf.write(endstr)
问题解答
回答1:filename_list = glob.glob(’.html’) + glob.glob(’.htm’)
相关文章:
1. angular.js - angularjs ng-repeat 不能刷新2. mac连接阿里云docker集群,已经卡了2天了,求问?3. docker镜像无法删除,请求解决办法。4. 老师,你这两条斜杠的是怎么注释的?5. PHP单例模式6. node.js - 来帮我捋一下node中fs模块watch实现原理7. angular.js - 用angular2-cli打包项目之后,跳转路由刷新会报404的错误8. docker 下面创建的IMAGE 他们的 ID 一样?这个是怎么回事????9. css3 - css字体样式加填充色而不是背景色10. PHP中的$this代表当前的类还是方法?