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’)
相關文章:
