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. docker api 开发的端口怎么获取?2. node.js - 我是一个做前端的,求教如何学习vue,node等js引擎?3. android glide asbitmap 在baseadpter中的问题4. docker网络端口映射,没有方便点的操作方法么?5. docker镜像push报错6. 老师,flex-shrink: 1; 按视频操作,不会自动缩放7. php - MySQL数据库设计,获取点赞的人数8. css3 - 微信前端页面遇到的transition过渡动画的bug9. javascript - Vue键盘事件为何要加上native?10. sublime-text - sublime text 3中编译python(sublimeREPL),如何仅运行单行/选定部分

网公网安备