對爬蟲數據進行自然語言清洗時用到的一些正則表達式
標簽中的所有屬性匹配(排除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 版權聲明:本文為博主原創文章,轉載請附上博文鏈接!