python - 關(guān)于代碼的優(yōu)化問題
問題描述
我新手寫的代碼,用來處理爬蟲下來的htm文件內(nèi)容,雖然解決問題,但是會有遺漏文件不處理。爬蟲是爬一些文章的網(wǎng)站下來的,和網(wǎng)頁另存為沒什么區(qū)別。
想大神們幫我看看我的代碼,怎么優(yōu)化不會有遺漏。比較小白的代碼,麻煩了!!!
# -*- 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’)
相關(guān)文章:
1. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.2. javascript - vue生成一維碼?求助!!!!!急3. docker-compose中volumes的問題4. java - SSH框架中寫分頁時service層中不能注入分頁類5. boot2docker無法啟動6. docker網(wǎng)絡(luò)端口映射,沒有方便點的操作方法么?7. 如何使用git對word文檔進行版本控制?8. java - Spring事務(wù)回滾問題9. javascript - mock.js可以存儲數(shù)據(jù)嗎10. nignx - docker內(nèi)nginx 80端口被占用

網(wǎng)公網(wǎng)安備