成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術文章
文章詳情頁

python - 關于代碼的優化問題

瀏覽:153日期:2022-08-17 09:32:17

問題描述

我新手寫的代碼,用來處理爬蟲下來的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(’&#8217;’, ’’, threetxt) fivetxt=re.sub(’&#8221;’, ’'’, fourtxt) sixtxt=re.sub(’&#8220;’, ’'’, fivetxt)endstr=re.sub(’&#8211;’, ’-’, 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’)

標簽: Python 編程
相關文章: