Python implements PHP-like strip_tags functions, and can be customized to set retention tags

Source: Internet
Author: User
Recently in the study of Python, found that use is still very unaccustomed, a lot of PHP inside a very simple function in Python have to look for half a day, and very versatile to achieve their own.

Today to do a collection, need to filter the contents of the label, made an afternoon, seemingly finally got out, testing the release to the expected effect, nonsense not to say that the code

from html.parser import htmlparserdef strip_tags (HTML, save=none): result = [] Start        = [] Data = [] def starttag (tag, attrs): If tag not in Save:return start.append (tag)                If attrs:j = 0 for attr in attrs:attrs[j] = attr[0] + ' = "' + attr[1] + '" '  J + = 1 Attrs = "+ (". Join (attrs)) Else:attrs = ' result.append ' (' < ')
 + tag + attrs + ' > ') def endtag (tag): if start and tag = = Start[len (start)-1]: Result.append (' !--' + tag + '--> ') parser = Htmlparser () Parser.handle_data = Result.append if Save:parser.handle_star Ttag = Starttag Parser.handle_endtag = Endtag parser.feed (HTML) parser.close () for I in range (0, Len (resul T): tmp = Result[i].rstrip (' \ n ') tmp = Tmp.lstrip (' \ n ') if Tmp:data.append (TMP) retur n '. Join (data) 

How to use:

    result = Strip_tags ("" "the anti-virus soccer score live game color Football basketball Competition" >

Hit the snake hit Seven inch, North single 7 string 1. As Beijing single-field SP value calculation rules and competition, 4 strings 1 and below betting to buy more cost-effective, and 7 strings more than 1 bets are likely to pay taxes, but not cost-effective. According to the calculation, Beijing single-field 4 strings from 1 to 7 strings between 1 betting the most cost-effective.

"" ", [' P ', ' img ']) print (Result)

Output Result:

Occurrence of anti-virus soccer score live game color Football basketball competition >

Hit the snake hit Seven inch, North single 7 string 1. As Beijing single-field SP value calculation rules and competition, 4 strings 1 and below betting to buy more cost-effective, and 7 strings more than 1 bets are likely to pay taxes, but not cost-effective. According to the calculation, Beijing single-field 4 strings from 1 to 7 strings between 1 betting the most cost-effective.


Reserved and only

Label

  • 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.