Python爬虫学习==>第九章:正则表达式基础
学习目的:
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特点字符、及这些特点字符组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。
正式步骤
Step1:常用匹配模式

Step2:最常规的匹配
import re testString = 'I have 4Learned the python years'
print(len(testString))
result = re.match('^I\s\w{4}\s\d\w{7}.*years$',testString)
print(result)
print(result.group()) #现实匹配结果
print(result.span() #现实匹配区间
运行结果:
32
<_sre.SRE_Match object; span=(0, 32), match='I have 4Learned the python years'>
I have 4Learned the python years
(0, 32)
范匹配:
.*可以把除了匹配的开头和结尾都匹配
import re
testString = 'I have 4Learned the python years'
print(len(testString))
result = re.match('^I.*years$',testString)
print(result)
print(result.group())
print(result.span())
匹配目标:
设置起始端点后,用()来把需要匹配的目标括号起来
import re testString = 'I have Learned the python years'
print(len(testString))
result = re.match('^I\s\w{4}\s(\w+)\s.*years$',testString)
print(result)
print(result.group(1))
print(result.span())
贪婪匹配:
import re
testString = 'I have 7777 Learned the python years'
print(len(testString))
result = re.match('^I.*(\d+).*years$',testString)
print(result)
print(result.group(1))
print(result.span())
运行结果:
36
<_sre.SRE_Match object; span=(0, 36), match='I have 7777 Learned the python years'>
7
(0, 36)
非贪婪匹配
import re
testString = 'I have 7777 Learned the python years'
print(len(testString))
result = re.match('^I.*?(\d+).*years$',testString)
print(result)
print(result.group(1))
print(result.span())
运行结果:
36
<_sre.SRE_Match object; span=(0, 36), match='I have 7777 Learned the python years'>
7777
(0, 36)
Step3:匹配模式
包含换行符:
import re
testString = '''I have 7777
Learned the python years'''
print(len(testString))
result = re.match('^I.*(\d+).*years$',testString,re.S)
print(result)
print(result.group(1))
print(result.span())
转义:
import re
content = "i have $5.00"
result = re.match('i have \$5\.00',content)
print(result.group())
Step4: re.search
功能:扫描整个字符串,返回第一个成功的匹配
import re
testString = '''I have 7777
Learned the python years'''
print(len(testString))
result = re.search('I.*(\d+).*years$',testString,re.S)
print(result)
print(result.group(1))
print(result.span())
总结:为了匹配方便,能用search就不用match,因为search方法不用限制匹配字符串的头部必须一致
Step5: re.compile
# -*- coding:utf-8 -*-
"""
re.compile 将一个正则表达式串编译成正则对象,以便于复用该匹配模式--简言之就是代码复用
按我的理解就是下面例子中的pattern就是过滤条件
""" import re content = "I love python"
pattern = re.compile('I.*python',re.S)
result = re.match(pattern,content)
result1 = pattern.match(content)
print(result.group())
print(result1.group())
运行结果:
I love python
I love python
学习总结:
正则表达式的应用需要多实践,在过滤爬取的数据时,应用较多
Python爬虫学习==>第九章:正则表达式基础的更多相关文章
- Python爬虫学习==>第二章:MongoDB环境配置
学习目的: MongoDB的安装 正式步骤 (VMWare 虚拟机上无法安装这个MongoDB的自启动服务,如果你能办到,请多赐教) Step1:MongoDB的简介 MongoDB是一个基于分布式文 ...
- Python爬虫学习==>第一章:Python3+Pip环境配置
前置操作 软件名:anaconda 版本:Anaconda3-5.0.1-Windows-x86_64清华镜像 下载链接:https://mirrors.tuna.tsinghua.edu.cn/ ...
- Python实战:Python爬虫学习教程,获取电影排行榜
Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...
- 《Python爬虫学习系列教程》学习笔记
http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...
- [转]《Python爬虫学习系列教程》
<Python爬虫学习系列教程>学习笔记 http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多. ...
- python爬虫学习(1) —— 从urllib说起
0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...
- Python爬虫学习:三、爬虫的基本操作流程
本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...
- Python爬虫学习系列教程
最近想学一下Python爬虫与检索相关的知识,在网上看到这个教程,觉得挺不错的,分享给大家. 来源:http://cuiqingcai.com/1052.html 一.Python入门 1. Pyth ...
- python爬虫学习 —— 总目录
开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...
随机推荐
- Java 实现的 简单WordCount功能
githup 链接:https://gitee.com/iy2524/WordCount.git PSP表格 psp2.1 psp阶段 估计耗时(分钟) 实际耗时(分钟) Planning 计 ...
- Google Protocol Buffer入门
简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...
- java 获取随机数的方法
方法一: (数据类型)(最小值 + Math.random()*(最大值-最小值+1) ); 示例: (int)(1+Math.random()*(10-1+1)): 获取int类型 1-10的随机数 ...
- springboot整合mongodb问题1-Decimal128和BigDecimal的转换之mongodb转换器使用(转)
转自:https://blog.csdn.net/weixin_41792559/article/details/79575524 1.Decimal128的了解由于mongodb4.3以上新加了De ...
- pro git 读书笔记 2
Git 2 - Git Basics 1 add github 上建立新的 repository,命名 demo git clone 到本地 github 目录 将自己之前的项目 copy 到该 de ...
- hdu 6065 RXD, tree and sequence
题 OwO http://acm.hdu.edu.cn/showproblem.php?pid=6065 (2017 Multi-University Training Contest - Team ...
- (转载)Google 发布 Android 性能优化典范
2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中 ...
- Prism框架中View与Region关联的几种方式
Prism.Regions命名空间下有2个重要接口:IRegionManager.IRegion IRegionManager接口中的方法与属性:AddToRegion().RegisterViewW ...
- Java进阶知识06 Hibernate一对一单向外键关联(Annotation+XML实现)
1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...
- Mockito 2 让我们校验一些行为
在下面的示例中,我们将会模拟(Mock)一个 List 列表. 这是因为绝大部分的人对列表这个接口比较熟悉(例如 add(), get(), clear() 方法). 在实际情况中,请不要 mock ...