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