Python string filtering

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.