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 - history.replaceState()無法改變query參數2. ios - 類似微博首頁,一張圖的時候是如何確定圖大小的?3. python小白 問關于類里面屬性的問題4. javascript - 圖片請求失敗怎么去掉左上角的小圖標?5. html5 - 如何解決bootstrap打開模態modal窗口引起頁面抖動?6. update方法不能更新字段值為0的數據7. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?8. 老哥們求助啊9. python - django模板 include模板的數據問題10. javascript - 有什么工具可以自動給css3的樣式 加 前綴 要最簡單的 不會sass less
