#encoding: utf--8

import re
import os
dic  = '重读历史'
for i in range(1,173):
    name = dic + '/' + str(i) + '.txt'
    with open(name,'r',encoding='utf-8') as file:
        buff = file.readlines()
        content = ''
        for item in buff:
            content += item
        # print(content)
        # <span style="font-family: 楷体_GB2312, 楷体;"><span style="font-size: 14px;">至元十九年，&ldquo;海盗们&rdquo;开启了中国海上漕运史前无古人的冒险之旅。此后他们又相继摸索出两条新航线，海运效率大增。顺利的话，最快10天就可以由浙江抵达大都。据《大元海运记》《元史&middot;食货志》统计，海运运粮数量呈阶梯式蹿升，最多时一年可达350万石。途中粮食损耗也由最初的25%下降到1%。海上漕运从此成为关乎元大都存亡的经济命脉。</span></span>
        buff = re.sub(r'<span style="font-family: 楷体_GB2312, 楷体;">.*</span></span>','',content)
        buff = re.sub(r'<p class="detailPic">.*<p class="picIntro">','',buff)
        buff = re.sub(r'<p class="detailPic" style="text-align: left;"> </p>','',buff)
        buff = re.sub(r'<p style="text-align:center"><img src=".*?" alt="" /></p>','',buff)
        buff = re.sub(r'<span style="font-family: 楷体_GB2312, 楷体;">','',buff)
        buff = re.sub(r'<p class="detailPic">.*?</p>','',buff)
        buff = re.sub(r'<p class=[\s\S]*?>?</p>','',buff)
        buff = re.sub(r'<strong>.*?<strong>','',buff)
        buff = re.sub(r'<strong>.*?</strong>', '', buff)
        buff = re.sub(r'<span>.*?</span>','',buff)
        buff = re.sub(r'<span style="text-indent: 2em;">','',buff)
        buff = re.sub(r'<p>本文摘自.*?</p>','',buff)
        buff = re.sub(r'<span class="ifengLogo">.*</span>','',buff)
        buff = re.sub(r'</span>','',buff)
        buff = re.sub(r'</strong>','',buff)
        buff = re.sub(r'<p>【.*?】</p>','',buff)
        buff = re.sub(r'<p>来源.*?</p>','',buff)
        buff = re.sub(r'<blockquote class=.*</blockquote>','',buff)
        buff = re.sub(r'<p><!--/enpcontent--><!----></p>','',buff)
        buff = re.sub(r'<span style="[\s\S]*?">','',buff)
        buff = re.sub(r'<p><!-- wemedia true --></p>','',buff)
        buff = re.sub(r'&ldquo;','\'',buff)
        buff = re.sub(r'<p>▼</p>','\'',buff)
        buff = re.sub(r'<span style="font-family:宋体;mso-ascii-font-family:&quot;Times New Roman&quot;;mso-hansi-font-family:&quot;Times New Roman&quot;;mso-font-kerning:0pt;mso-ansi-language:ZH-CN">','\'',buff)
        buff = re.sub(r'<h2></h2>','',buff)
        buff = re.sub(r'<strong style="line-height: 24px; [\s\S]*?>','',buff)
        buff = re.sub(r'<strong style="line-height: 24px; color: rgb(43, 43, 43); font-family: simsun, arial, helvetica, clean, sans-serif; background-color: rgb(255, 255, 255);">','',buff)
        buff = re.sub(r'<p><!--mainContent begin--></p><p class="duanluo">','',buff)
        buff = re.sub(r'<p><!--mainContent begin--></p>','',buff)
        buff = re.sub(r'&rdquo;','\'',buff)
        buff = re.sub(r'&mdash;','-',buff)
        buff = re.sub(r'&nbsp;',' ',buff)
        buff = re.sub(r'&quot;','"',buff)
        buff = re.sub(r'&middot;','·',buff)
        buff = re.sub(r'&hellip;','',buff)
        buff = re.sub(r'&diams;','',buff)
        buff = re.sub(r'&rsquo;','"',buff)
        buff = re.sub(r'&lsquo;','"',buff)
        buff = re.sub(r'p><!--/enpcontent--></p>','',buff)
        buff = re.sub(r'<a href=.*>.*?</a>','',buff)
        buff = re.sub(r'<p class="detailPic" style="text-align: left;"> </p>','',buff)
        buff = re.sub(r'<p class="detailPic" style="text-align: left;">','',buff)
        buff = re.sub(r'<strong style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;">','',buff)
        buff = re.sub(r'<br style="max-width: 100%;box-sizing: border-box;word-wrap: break-word !important;" />','',buff)
        buff = re.sub(r'<p style="text-align:[\s\S]*?">','',buff)
        buff = re.sub(r'<strong style="margin: 0px; padding: 0px;">','',buff)
        buff = re.sub(r'<strong style=[\s\S]*?>','',buff)
        buff = re.sub(r'<span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">','',buff)
        buff = re.sub(r'<p style="text-align: center;"> </p>','',buff)
        buff = re.sub(r'<p><!--enpproperty.*<!--enpcontent-->.*?</p>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<p><!--enpproperty.*<!--enpcontent--></p>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<date>.*?</date>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<articleid>.*?</articleid>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<author>.*?</author>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<title>.*?</title>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<subtitle>.*?</subtitle>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<nodename>.*?</nodename>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<nodesearchname>.*?</nodesearchname>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<introtitle>.*?</introtitle>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<siteid>.*?</siteid>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<nodeid>.*?</nodeid>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<keyword>.*?</keyword>','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<!--enpcontent-->','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<!--enpproperty.[\s\S]*?enpproperty-->','',buff,flags=re.MULTILINE)
        buff = re.sub(r'<p>[\s\S]{0,1}</p>','',buff)
        buff = re.sub(r'<p><!--mainContent end--></p>','',buff)
        buff = re.sub(r'<p>长按右图识别二维码一键关注</p>','',buff)
        buff = re.sub(r'<script[\s\S]*?>[\s\S]*?</script>','',buff)
        buff = re.sub(r'<br />','',buff)
        buff = re.sub(r'<div[\s\S]*?>','',buff)
        buff = re.sub(r'<div>?</div>','',buff)
        buff = re.sub(r'</div>','',buff)
        buff = re.sub(r'</div>','',buff)
        buff = re.sub(r'<p class="wx"></p>','',buff)
        buff = re.sub(r'<p><style type="text/css">[\s\S]*</style></p>','',buff)
        buff = re.sub(r'<p>有趣味、有思维、有品位的\'三味\'历史，请关注微信公众号：mashaohua108</p>','',buff)
        buff = re.sub(r'<img src=[\s\S]*?>','',buff)
        buff = re.sub(r'<table[\s\S]*?>[\s\S]*?</table>','',buff)      
        buff = re.sub(r'&quot;','"',buff)
        buff = re.sub(r'&lt;','<',buff)
        buff = re.sub(r'&gt;','>',buff)
        buff = re.sub(r'<strong style=[\s\S]*?>','',buff)
        buff = re.sub(r'([\s\S]*?)','',buff)
        buff = re.sub(r'（[\s\S]*?）','',buff)
        buff = re.sub(r'&darr','',buff)
        buff = re.sub(r'<script src=[\s\S]*?></script>','',buff)
        buff = re.sub(r'<!--底部广告位end-->','',buff)
        buff = re.sub(r'<b>.*?</b>','',buff)
        buff = re.sub(r'图片来源：','',buff)
        #去除所有单引号
        buff = re.sub(r'\'','',buff)
        buff = re.sub(r'\"','',buff)
        buff = re.sub(r'-','-',buff)
        #多字符
        buff = re.sub(r'-{2,10}','-',buff)
        buff = re.sub(r'\.{2,10}','.',buff)
        buff = re.sub(r'。{2,10}','。',buff)
        buff = re.sub(r'？{2,10}','？',buff)
        buff = re.sub(r'！{2,10}','！',buff)
        #去除所有的~
        #去除所有的括号
        buff = re.sub(r'\(.*?\)','',buff)
        buff = re.sub(r'（.*?）','',buff)
        #替换数字
        buff = re.sub(r'1','一',buff)
        buff = re.sub(r'2','二',buff)
        buff = re.sub(r'3','三',buff)
        buff = re.sub(r'4','四',buff)
        buff = re.sub(r'5','五',buff)
        buff = re.sub(r'6','六',buff)
        buff = re.sub(r'7','七',buff)
        buff = re.sub(r'8','八',buff)
        buff = re.sub(r'9','九',buff)
        buff = re.sub(r'0','零',buff)
        buff = re.sub(r'一一月','十一月',buff)
        buff = re.sub(r'一二月','十二月',buff)
        buff = re.sub(r'二二世纪','二十二世纪',buff)
        buff = re.sub(r'二一世纪','二十一世纪',buff)
        buff = re.sub(r'二零世纪','二十世纪',buff)
        #全角半角
        buff = re.sub(r'\,','，',buff)
        buff = re.sub(r':','：',buff)
        buff = re.sub(r'\?','？',buff)
        buff = re.sub(r'\!','！',buff)
        #去除英文字符
        buff = re.sub(r'[A-Za-z]*','',buff)
        buff = re.sub(r'<span style=[\s\S]*?>','',buff)
    with open('chongdulishi.txt','a',encoding='utf-8') as destfile:

        destfile.writelines(buff)