您的位置:首页技术文章
文章详情页

python - 关于代码的优化问题

【字号: 日期:2022-08-17 09:32:17浏览:54作者:猪猪

问题描述

我新手写的代码,用来处理爬虫下来的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(’&#8217;’, ’’, threetxt) fivetxt=re.sub(’&#8221;’, ’'’, fourtxt) sixtxt=re.sub(’&#8220;’, ’'’, fivetxt)endstr=re.sub(’&#8211;’, ’-’, 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’)

标签: Python 编程
相关文章: