# -*- coding: utf-8 -*-
import re
class Tool():
# 去除img標籤,7位長空格
removeImg = re.compile('<img.*?>| {7}|')
# 刪除span標籤
removeSpan = re.compile('<span.*?>| {7}|')
# 刪除超連接標籤
removeAddr = re.compile('<a.*?>|</a>')
# 把換行的標籤換爲\n
replaceLine = re.compile('<tr>|<div>|</div>|</p>')
# 將表格製表<td>替換爲\t
replaceTD = re.compile('<td>')
# 把段落開頭換爲\n加空兩格
replacePara = re.compile('<p.*?>')
# 將換行符或雙換行符替換爲\n
replaceBR = re.compile('<br><br>|<br>')
# 將其他標籤剔除
removeExtraTag = re.compile('<.*?>')
def replace(self, content):
content = re.sub(self.removeImg, "", content)
content = re.sub(self.removeSpan, "", content)
content = re.sub(self.removeAddr, "", content)
content = re.sub(self.replaceLine, "\n", content)
content = re.sub(self.replaceTD, "\t", content)
content = re.sub(self.replacePara, "\n ", content)
content = re.sub(self.replaceBR, "\n", content)
content = re.sub(self.removeExtraTag, "", content)
# strip()將先後多餘內容刪除
return content.strip()