正则删除含有指定内容的元素

def regex_filter():
	html_str = '''<div class="cont-cont">
            <p></p><table cellspacing="0" cellpadding="0" width="90%" style="border:1px solid #000000; text-align:center; border-collapse:collapse;" align="center"><tbody><tr style="background-color: rgb(217, 217, 217);" class="firstRow"><td width="98" height="25" style="border:1px solid #000000;"><p><strong>产品名称</strong></p></td><td width="198" colspan="2" height="25" style="border:1px solid #000000;"><p><strong>价格类型</strong></p></td><td width="142" height="25" style="border:1px solid #000000;"><p><strong>价格</strong></p></td><td width="123" height="25" style="border:1px solid #000000;"><p><strong><span style="color: black;">涨跌</span></strong></p></td><td width="68" height="25" style="border:1px solid #000000;"><p><strong>单位</strong></p></td></tr><tr style=";height:45px"><td width="98" rowspan="10" height="45" style="border:1px solid #000000;"><p><strong>醋</strong></p><p><strong>酸</strong></p><p><strong>丁</strong></p><p><strong>酯</strong></p></td><td width="85" rowspan="2" height="45" style="border:1px solid #000000;"><p>外盘</p></td><td width="75" height="45" style="border:1px solid #000000;"><p>FOB美国海湾</p></td><td width="142" height="45" style="border:1px solid #000000;"><p>2639-2661</p></td><td width="123" height="45" style="border:1px solid #000000;"><p>0</p></td><td width="68" height="45" style="border:1px solid #000000;"><p>美元/吨</p></td></tr><tr style=";height:25px"><td width="75" height="25" style="border:1px solid #000000;"><p>FOB鹿特丹</p></td><td width="142" height="25" style="border:1px solid #000000;"><p>1968.75-1969.25</p></td><td width="123" height="25" style="border:1px solid #000000;"><p>0</p></td><td width="68" height="25" style="border:1px solid #000000;"><p>美元/吨</p></td></tr><tr style=";height:25px"><td width="85" rowspan="4" height="25" style="border:1px solid #000000;"><p>厂家</p></td><td width="75" height="25" style="border:1px solid #000000;"><p>江门谦信</p></td><td width="142" height="25" style="border:1px solid #000000;"><p>11800</p></td><td width="123" height="25" style="border:1px solid #000000;"><p>0</p></td><td width="68" height="25" style="border:1px solid #000000;"><p>元/吨</p></td></tr><tr style=";height:25px"><td width="75" height="25" style="border:1px solid #000000;"><p>江阴百川</p></td><td width="142" height="25" style="border:1px solid #000000;"><p>-</p></td><td width="123" height="25" style="border:1px solid #000000;"><p>-</p></td><td width="68" height="25" style="border:1px solid #000000;"><p>元/吨</p></td></tr><tr style=";height:25px"><td width="75" height="25" style="border:1px solid #000000;"><p>金沂蒙</p></td><td width="142" height="25" style="border:1px solid #000000;"><p>11500</p></td><td width="123" height="25" style="border:1px solid #000000;"><p>0</p></td><td width="68" height="25" style="border:1px solid #000000;"><p>元/吨</p></td></tr><tr style=";height:62px"><td width="75" height="62" style="border:1px solid #000000;"><p>东营益盛</p></td><td width="142" height="62" style="border:1px solid #000000;"><p>11600</p></td><td width="123" height="62" style="border:1px solid #000000;"><p><span style="color: red;">+100</span></p></td><td width="68" height="62" style="border:1px solid #000000;"><p>元/吨</p></td></tr><tr style=";height:25px"><td width="85" rowspan="3" height="25" style="border:1px solid #000000;"><p>市场</p></td><td width="75" height="25" style="border:1px solid #000000;"><p>华东市场</p></td><td width="142" height="25" style="border:1px solid #000000;"><p>11500-11600</p></td><td width="123" valign="top" height="25" style="border:1px solid #000000;"><p>0/0</p></td><td width="68" height="25" style="border:1px solid #000000;"><p>元/吨</p></td></tr><tr style=";height:25px"><td width="75" height="25" style="border:1px solid #000000;"><p>华南市场</p></td><td width="142" height="25" style="border:1px solid #000000;"><p>11700-11800</p></td><td width="123" valign="top" height="25" style="border:1px solid #000000;"><p><span style="color: red;">+400</span>/<span style="color:red">+500</span></p></td><td width="68" height="25" style="border:1px solid #000000;"><p>元/吨</p></td></tr><tr style=";height:25px"><td width="75" height="25" style="border:1px solid #000000;"><p>华北市场</p></td><td width="142" height="25" style="border:1px solid #000000;"><p>11400-11500</p></td><td width="123" valign="top" height="25" style="border:1px solid #000000;"><p><span style="color: red;">+400</span>/<span style="color:red">+300</span></p></td><td width="68" height="25" style="border:1px solid #000000;"><p>元/吨</p></td></tr><tr style=";height:25px"><td width="85" height="25" style="border:1px solid #000000;"><p>市场简述</p><p>及后市预测</p></td><td width="446" colspan="4" height="25" style="border:1px solid #000000;" align="left" valign="top"><p style="text-indent: 2em;">今日国内醋酸丁酯市场走势上扬,日内正丁醇市场走高,成本支撑下,业者心态坚挺,局部行情继续上行,场内实际交投一般,业者对高价多维持观望心态,预计短期国内醋酸丁酯市场或横盘整理,仍需关注原料价格走势以及现货供应情况。</p></td></tr></tbody></table><p style="text-indent: 2em;">2022年开春之际,中宇资讯特在江苏·南京组织“中国化工行业春季行情展望会(暨2022年化工品商贸流通启动大会)”,会议旨在搭建平台,促进交流,诚邀您的参与!</p><p style="text-indent: 2em;">报名电话:0533-7010346</p><p style="text-indent: 2em;">报名电话:0533-7010346</p><p style="text-indent: 2em;"><span style="text-indent: 2em;">详情请点击:<a href="http://weixin://resourceid/blank" target="_self">http://weixin://resourceid/blank</a></span></p><p style="line-height: 1.5em;"><br></p><p></p>
            <br>
            <br>
            <br>            
        </div>'''
	remove_note = re.compile('<p .*(报名电话|详情请点击|详情关注|化工品商贸流通启动大会).*</p>')
	html_str = re.sub(remove_note, '', html_str)
	print(html_str)

  如上所示 想要删除 p标签里含有报名电话、详情请点击、详情关注、化工品商贸流通启动大会 这些字符的时候就把这些标签删除  利用sub函数

posted @ 2022-02-14 17:02  kakaok  阅读(172)  评论(0)    收藏  举报