python zip|python strip_tags过滤html字符程序

更新时间:2019-08-09    来源:python    手机版     字体:

【www.bbyears.com--python】

例子

 代码如下

## Remove xml style tags from an input string.
#
#  @param string The input string.
#  @param allowed_tags A string to specify tags which should not be removed.
def strip_tags(string, allowed_tags=""):
  if allowed_tags != "":
    # Get a list of all allowed tag names.
    allowed_tags_list = re.sub(r"[\\/<> ]+", "", allowed_tags).split(",")
    allowed_pattern = ""
    for s in allowed_tags_list:
      if s == "":
       continue;
      # Add all possible patterns for this tag to the regex.
      if allowed_pattern != "":
        allowed_pattern += "|"
      allowed_pattern += "<" + s + " [^><]*>$|<" + s + ">|"
    # Get all tags included in the string.
    all_tags = re.findall(r"<]+>", string, re.I)
    for tag in all_tags:
      # If not allowed, replace it.
      if not re.match(allowed_pattern, tag, re.I):
        string = string.replace(tag, "")
  else:
    # If no allowed tags, remove all.
    string = re.sub(r"<[^>]*?>", "", string)
  return string


测试

 代码如下

>>> strip_tags("Hello World!


")
"Hello World! "
>>> strip_tags("Hello World!
", "")
"Hello World!"
>>> strip_tags("Hello World!
", ",
")
"Hello World!
"
>>>

本文来源:http://www.bbyears.com/jiaocheng/61085.html

猜你感兴趣

热门标签

更多>>

本类排行