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. javascript - 有適合開發手機端Html5網頁小游戲的前端框架嗎?2. javascript - 關于apply()與call()的問題3. python 利用subprocess庫調用mplayer時發生錯誤4. python - Pycharm的Debug用不了5. python - pandas按照列A和列B分組,將列C求平均數,怎樣才能生成一個列A,B,C的dataframe6. html - eclipse 標簽錯誤7. 安全性測試 - nodejs中如何防mySQL注入8. javascript - nginx反向代理靜態資源403錯誤?9. python - pycharm 自動刪除行尾空格10. python文檔怎么查看?
