Чистка HTML созданного в MS Word
????-?? ?? ???? ?????... ?? ??... ???? ??????? ????????? ????, ????? ????????? ???????????? ? ????? HTML ???-?? ?????????? ? MS Word... ??? ??? ???? Save As Filtered HTML ?? ????????. ??, ?? ????????? ? ???? ??????????, ??? ????? ? ?????? ???????? ????? ?????? ????? ??? ??????
<o:DocumentProperties>
<o:Author>Eldar Musayev</o:Author>
<o:Revision>4</o:Revision>
<o:TotalTime>179</o:TotalTime>
<o:Created>2008-06-03T05:31:00Z</o:Created>
<o:LastSaved>2008-06-03T06:59:00Z</o:LastSaved>
<o:Pages>1</o:Pages>
<o:Words>1794</o:Words>
<o:Characters>10226</o:Characters>
<o:Company>Home</o:Company>
<o:Lines>85</o:Lines>
<o:Paragraphs>23</o:Paragraphs>
<o:CharactersWithSpaces>11997</o:CharactersWithSpaces>
<o:Version>11.9999</o:Version>
</o:DocumentProperties>
<o:OfficeDocumentSettings>
<o:DoNotRelyOnCSS/>
<o:DoNotOrganizeInFolder/>
</o:OfficeDocumentSettings>
</xml><![endif]--><!--[if gte mso 9]><xml>
??? ???
<font size=5 color="#345a8a" face=Calibri><span lang=RU
style='font-size:16.0pt;mso-ansi-language:RU'>...<o:p></o:p></span></font>
??????? ?????????? ?????????? ?? ?????. ???, ???????, ???????? ? ?????? ? ???????? ?? ?????????... ????? 60 ????? ??????????, ? HTML ?????????? ??????????, ??????????, ?????? ????????... ? ?????, ???? ???? - ???????????! ? ??, ??? ?????? ? ????????????? ?????...
import sys
import HTMLParserclass HtmlCleaner(HTMLParser.HTMLParser):
def __init__(self):
self.stuff = ""
self.sep = ""
self.on = False
HTMLParser.HTMLParser.reset(self)def checktag(self,tag):
res = 0
if tag=="p" or tag=="br" or tag=="hr" or tag=="b" or tag=="i" or tag=="h1" or tag=="h2" or tag=="h3":
res = 1
elif tag=="img" or tag=="a":
res = 2
return resdef flushStuff(self):
if not self=="":
print self.stuff.lstrip()
self.stuff = ""def getAttr(self,attrs,name):
res = ""
for attr in attrs:
if (attr[0]==name):
res = " " + name + "=\"" + attr[1] + "\""
return res
def handle_starttag(self,tag,attrs):
if (tag=="body"):
self.on = True
if self.on:
cl = self.checktag(tag)
if cl==1:
print "<" + tag + ">"
elif cl==2:
if tag=="a":
self.flushStuff()
print "<" + tag + self.getAttr(attrs,"href") + ">"
elif tag=="img":
self.flushStuff()
print "<" + tag + self.getAttr(attrs,"src") + self.getAttr(attrs,"width") + self.getAttr(attrs,"height") + self.getAttr(attrs,"border") + "\">"def handle_endtag(self,tag):
if self.on:
cl = self.checktag(tag)
if cl>0:
self.flushStuff()
print "</" + tag + ">"def handle_data(self,data):
if self.on:
data = data.replace("\r"," ").replace("\n"," ").replace("\t"," ")
if data[0]==" ":
self.stuff = self.stuff + " "
self.stuff = self.stuff + data.strip()
if data[len(data)-1:]==" ":
self.stuff = self.stuff + " "
cleaner = HtmlCleaner()
doc = file(sys.argv[1],"r+")
for line in doc:
line = line.replace("<![","<!--").replace("]>","-->")
cleaner.feed(line)doc.close()