美文网首页数据清洗
「Python」数据清洗常用正则

「Python」数据清洗常用正则

作者: HughDong | 来源:发表于2018-05-13 22:07 被阅读84次

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

    <html>标签中的所有属性匹配(排除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"']
    

    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;"']
    

    非中文字符

    # u'[^\u4e00-\u9fa5]+'
    # 清除非中文字符
    
    str1 = 'aa.,a中文,aa。a'
    
    print(re.compile(u"[^\u4e00-\u9fa5]+").sub('', str1))
    # result: 中文
    

    指定通配符中的内容

    # \{.*?\} // 匹配{}中的内容
    # \<.*?\> // 匹配<>中的内容
    
    str1 = '{通配符}你好,今天开学了{通配符},你好'
    print(re.compile(r'\{.*?\}').sub('', str1))
    # result: 你好,今天开学了,你好
    

    html标签尾部的空格

    # \s*(?=\>)
    

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

    # \<style.*?/style\>
    

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

    # 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)
    

    相关文章

      网友评论

        本文标题:「Python」数据清洗常用正则

        本文链接:https://www.haomeiwen.com/subject/rbdrdftx.html