zoukankan      html  css  js  c++  java
  • 爬虫常用正则、re.findall 使用

    爬虫常用正则

    爬虫经常用到的一些正则,这可以帮助我们更好地处理字符。

    正则符

    单字符

    . : 除换行以外所有字符
    [] :[aoe] [a-w] 匹配集合中任意一个字符
    d :数字  [0-9]
    D : 非数字
    w :数字、字母、下划线、中文
    W : 非w
    s :所有的空白字符包,括空格、制表符、换页符等等。等价于 [ f
    
    	v]
    S : 非空白

    数量修饰

    * : 任意多次  >=0
    + : 至少1次   >=1
    ? : 可有可无  0次或者1次
    {m} :固定m次 hello{3,}
    {m,} :至少m次
    {m,n} :m-n次

    边界

    $ : 以某某结尾 
    ^ : 以某某开头

    分组

    (ab)

    贪婪模式

    .*

    非贪婪惰性模式

    .*?
    # 1 提取出python
    '''
    key = 'javapythonc++php'
    
    re.findall('python',key)
    re.findall('python',key)[0]
    '''
    # 2 提取出 hello word
    '''
    key = '<html><h1>hello word</h1></html>'
    print(re.findall('<h1>.*</h1>', key))
    print(re.findall('<h1>(.*)</h1>', key))
    print(re.findall('<h1>(.*)</h1>', key)[0])
    '''
    # 3 提取170
    '''
    key = '这个女孩身高170厘米'
    print(re.findall('d+', key)[0])
    '''
    # 4 提取出http://和https://
    '''
    key = 'http://www.baidu.com and https://www.cnblogs.com'
    print(re.findall('https?://', key))
    '''
    # 5 提取出 hello
    '''
    key = 'lalala<hTml>hello</HtMl>hahaha'   # 输出的结果<hTml>hello</HtMl>
    print(re.findall('<[hH][tT][mM][lL]>.*[/hH][tT][mM][lL]>',key))
    '''
    # 6 提取hit. 贪婪模式;尽可能多的匹配数据
    '''
    key = 'qiang@hit.edu.com'                # 加?是贪婪匹配,不加?是非贪婪匹配
    print(re.findall('h.*?.', key))
    '''
    # 7 匹配出所有的saas和sas
    '''
    key = 'saas and sas and saaas'
    print(re.findall('sa{1,2}s',key))
    '''
    # 8 匹配出 i 开头的行
    '''
    key = """fall in love with you
    i love you very much 
    i love she
    i love her
    """
    print(re.findall('^i.*', key, re.M))
    '''
    # 9 匹配全部行
    '''
    key = """
    <div>细思极恐
    你的队友在看书,
    你的闺蜜在减肥,
    你的敌人在磨刀,
    隔壁老王在练腰.
    </div>
    """
    print(re.findall('.*', key, re.S))
    '''
    案例题

    re.findall 使用

    1、re.findall 可以对多行进行匹配,并依据参数作出不同结果。

    re.findall(取值,值,re.M)
        - re.M :多行匹配
        - re.S :单行匹配 如果分行则显示/n
        - re.I : 忽略大小写
        - re.sub(正则表达式, 替换内容, 字符串)
  • 相关阅读:
    Python之异常篇 [待更新]
    python脚本工具 - 4 获取系统当前时间
    python脚本工具 - 3 目录遍历
    数字签名和数字证书到底是个神马玩意?
    CSRF攻击[转]
    Python之数据结构篇
    Python之模块篇
    Python之函数篇
    python脚本工具-2 去除扩展名后提取目录下所有文件名并保存
    python脚本工具-1 制作爬虫下载网页图片
  • 原文地址:https://www.cnblogs.com/xiangsikai/p/11251620.html
Copyright © 2011-2022 走看看