python實(shí)現(xiàn)html轉(zhuǎn)ubb代碼(html2ubb)
這兩天在用python寫(xiě)一個(gè)采集器,有個(gè)功能模塊是html代碼轉(zhuǎn)換為ubb,網(wǎng)上貌似沒(méi)有現(xiàn)成程序,就自己寫(xiě)了個(gè)函數(shù),順便鍛煉下自己的正則。
import re def Html2UBB(content): #以下是將html標(biāo)簽轉(zhuǎn)為ubb標(biāo)簽 pattern = re.compile( '<a href=\"([sS]+?)\"[^>]*>([sS]+?)</a>',re.I) content = pattern.sub(r'[url=1]2[/url]',content) pattern = re.compile( '<img[^>]+src=\"([^\"]+)\"[^>]*>',re.I) content = pattern.sub(r'[img]1[/img]',content) pattern = re.compile( '<strong>([sS]+?)</strong>',re.I) content = pattern.sub(r'[b]1[/b]',content) pattern = re.compile( '<font color=\"([sS]+?)\">([sS]+?)</font>',re.I) content = pattern.sub(r'[1]2[/1]',content) pattern = re.compile( '<[^>]*?>',re.I) content = pattern.sub('',content) #以下是將html轉(zhuǎn)義字符轉(zhuǎn)為普通字符 content = content.replace('<','<') content = content.replace('>','>') content = content.replace('”','”') content = content.replace('“','“') content = content.replace('"','"') content = content.replace('©','©') content = content.replace('®','®') content = content.replace(' ',' ') content = content.replace('—','—') content = content.replace('–','–') content = content.replace('‹','‹') content = content.replace('›','›') content = content.replace('…','…') content = content.replace('&','&') return content
使用時(shí)直接調(diào)用Html2UBB函數(shù),返回值就是ubb碼了html轉(zhuǎn)ubb
- 打印出python 當(dāng)前全局變量和入口參數(shù)的所有屬性
- Python實(shí)現(xiàn)端口復(fù)用實(shí)例代碼
- 采用python實(shí)現(xiàn)簡(jiǎn)單QQ單用戶(hù)機(jī)器人的方法
- python實(shí)現(xiàn)定制交互式命令行的方法
- Python 獲取新浪微博的最新公共微博實(shí)例分享
- python實(shí)現(xiàn)的解析crontab配置文件代碼
- Python開(kāi)發(fā)WebService系列教程之REST,web.py,eurasia,Django
- windows下wxPython開(kāi)發(fā)環(huán)境安裝與配置方法
- Python實(shí)現(xiàn)全局變量的兩個(gè)解決方法
相關(guān)文章
python檢測(cè)遠(yuǎn)程udp端口是否打開(kāi)的方法
這篇文章主要介紹了python檢測(cè)遠(yuǎn)程udp端口是否打開(kāi)的方法,涉及Python操作socket實(shí)現(xiàn)檢測(cè)udp端口的技巧,需要的朋友可以參考下2015-03-03將Pytorch模型從CPU轉(zhuǎn)換成GPU的實(shí)現(xiàn)方法
今天小編就為大家分享一篇將Pytorch模型從CPU轉(zhuǎn)換成GPU的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python中requests、aiohttp、httpx性能比拼
本文主要介紹了Python中requests、aiohttp、httpx性能比拼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06flask中主動(dòng)拋出異常及統(tǒng)一異常處理代碼示例
這篇文章主要介紹了flask中主動(dòng)拋出異常及統(tǒng)一異常處理代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01