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安裝后出現Cannot connect to the Docker daemon.2. 為什么我ping不通我的docker容器呢???3. android - webview 自定義加載進度條4. mysql - 怎么讓 SELECT 1+null 等于 15. javascript - 微信音樂分享6. 網頁爬蟲 - 用Python3的requests庫模擬登陸Bilibili總是提示驗證碼錯誤怎么辦?7. javascript - 微信小程序封裝定位問題(封裝異步并可能多次請求)8. 并發模型 - python將進程池放在裝飾器里為什么不生效也沒報錯9. linux - openSUSE 上,如何使用 QQ?10. python 怎樣用pickle保存類的實例?
