Demand:
STR1 = "
"" "<div class=" M_wrap clearfix "><ul class=" Clearfix "><br/><br/><
br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>< B
R/><br/><br/><br/><br/><br/><li class= "Li_1 clearfix" ><spa
n class= "Pt_title s_txt2" > Company: </span><span class= "Pt_detail" ><a href= "H
Location: Jiangsu, Yangzhou <br/> </span></li></ul></div></div></div></div> "" "
"
Want to remove all the tags of this string, such as removing </li>, </ul>, </div> Keep only the content without <>, but keep the <br/>
Is there any good way to do it? Using the regular can do the job:
# Coding:utf-8
Import renewline="""<div class= "M_wrap clearfix" ><ul class= "Clearfix" ><br/><br/><br/><br/><br/ ><br/><br/><br/><br/><br/><br/><br/><br
/><br/><br/><br/><br/><br/><li class= "Li_1 clearfix" ><span class= "Pt_ Title S_txt2 "> Company: </span><span class=" Pt_detail "><a
Href= "Http://s.weibo.com/user/&work=%E6%89%AC%E5%B7%9E%E6%8A%A5%E4%B8%9A%E9%9B%86%E5%9B%A2&from=inf &wvr=5&loc=infjob "target=" _blank ">
Yangzhou Newspaper group </a><br/> area: Jiangsu, Yangzhou <br/> </span></li></ul></div></div></ Div></div>"""
NewLine= Newline.replace ('<br/>','!!! ###') Re_comment= Re.compile ('<[^>]*>') newlines= Re_comment.sub ("', newline) newlines= Newlines.replace ('!!! ###','<br/>'). Replace ('<br/><br/>','<br/>'). Replace ('<br/><br/>','<br/>')
PrintNewlines
The output is:
C:\Python27\python.exe f:/squid_frame/zyxt__weibo/test.py<br/> Company: Yangzhou Press group <br/> region: Jiangsu, Yangzhou <br /> Process finished with exit code 0
Python string filtering