「Python」数据清洗常用正则

对爬虫数据进行自然语言清洗时用到的一些正则表达式

标签中的所有属性匹配(排除src,href等指定参数)

参考链接

# \b(?!src|href)\w+=[\'\"].*?[\'\"](?=[\s\>])
# 匹配特征 id="..." 
# \b(?!...)排除属性名中的指定参数,零宽断言前向界定判断属性结束
# tips: 带\b的python正则匹配一定要加r转义

str1 = '''
<div class="concent" id="zoomcon" style="padding:15px;"> <img border="0" src="/xcsglj/zyhd/201802/f5492c1752094f44bcebae4a68480c64/images/9a900610afc54ee3b468780785a2ecec.gif"> <img border="0" src="/xcsglj/zyhd/201802/f5492c1752094f44bcebae4a68480c64/images/4b802f5d2d8c4ecd9a0525e0da7d886e.gif"> <img href="0" src="/xcsglj/zyhd/201802/f5492c1752094f44bcebae4a68480c64/images/4b802f5d2d8c4ecd9a0525e0da7d886e.gif"> ''' print(re.findall(r'\b(?!src)\w+=[\'\"].*?[\'\"](?=[\s\>])', string=str1)) # result: ['class="concent"', 'id="zoomcon"', 'style="padding:15px;"', 'border="0"', 'border="0"', 'href="0"']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

html标签的所有参数

# (?<=\<\w{1}\s).*?(?=\>)
# (?<=\<\w{2}\s).*?(?=\>)
# ...
# 清除n个字母的标签的所有参数
# tips: 零宽断言不支持不定长度的匹配 str1 = ''' <a class="1" id="1" style="padding:1;"> <td class="2" id="2" style="padding:2;"> <div class="3" id="3" style="padding:3;"> <span class="4" id="4" style="padding:4;"> <table class="5" id="5" style="padding:5;"> ''' print(re.findall('(?<=\<\w{1}\s).*?(?=\>)', string=str1)) # result: ['class="1" id="1" style="padding:1;"'] print(re.findall('(?<=\<\w{2}\s).*?(?=\>)', string=str1)) # result: ['class="2" id="2" style="padding:2;"'] print(re.findall('(?<=\<\w{3}\s).*?(?=\>)', string=str1)) # result: ['class="3" id="3" style="padding:3;"'] print(re.findall('(?<=\<\w{4}\s).*?(?=\>)', string=str1)) # result: ['class="4" id="4" style="padding:4;"'] print(re.findall('(?<=\<\w{5}\s).*?(?=\>)', string=str1)) # result: ['class="5" id="5" style="padding:5;"']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

非中文字符

# u'[^\u4e00-\u9fa5]+'
# 清除非中文字符

str1 = 'aa.,a中文,aa。a'

print(re.compile(u"[^\u4e00-\u9fa5]+").sub('', str1)) # result: 中文
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

指定通配符中的内容

# \{.*?\} // 匹配{}中的内容
# \<.*?\> // 匹配<>中的内容

str1 = '{通配符}你好,今天开学了{通配符},你好'
print(re.compile(r'\{.*?\}').sub('', str1)) # result: 你好,今天开学了,你好
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

html标签尾部的空格

# \s*(?=\>)
  • 1

指定标签(包括中间的内容)

# \<style.*?/style\>
  • 1

清除常用中英文字符/标点/数字外的特殊符号

# u'[^\u4e00-\u9fa5\u0041-\u005A\u0061-\u007A\u0030-\u0039\u3002\uFF1F\uFF01\uFF0C\u3001\uFF1B\uFF1A\u300C\u300D\u300E\u300F\u2018\u2019\u201C\u201D\uFF08\uFF09\u3014\u3015\u3010\u3011\u2014\u2026\u2013\uFF0E\u300A\u300B\u3008\u3009\!\@\#\$\%\^\&\*\(\)\-\=\[\]\{\}\\\|\;\'\:\"\,\.\/\<\>\?\/\*\+\_"\u0020]+' str1 = re\ .compile(\ u "[^" u "\u4e00-\u9fa5" u "\u0041-\u005A" u "\u0061-\u007A" u "\u0030-\u0039" u "\u3002\uFF1F\uFF01\uFF0C\u3001\uFF1B\uFF1A\u300C\u300D\u300E\u300F\u2018\u2019\u201C\u201D\uFF08\uFF09\u3014\u3015\u3010\u3011\u2014\u2026\u2013\uFF0E\u300A\u300B\u3008\u3009" u "\!\@\#\$\%\^\&\*\(\)\-\=\[\]\{\}\\\|\;\'\:\"\,\.\/\<\>\?\/\*\+\_" u "\u0020" u "]+")\ .sub('', str1)

--------------------- 作者:Hugh_Dong 来源:CSDN 原文:https://blog.csdn.net/qq_33282586/article/details/80643817?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/luyanru66/p/9761453.html