爬虫笔记:正则表达式(五)

写了怎么发请求和获取到数据,接下来就是该怎么处理数据了,打开一个网站之后,它会返回很多数据,数据很多,有很多都是咱们不需要的,咱们写爬虫的话只获取到对咱们自己有用的数据,就要从返回的数据里面找到咱们需要的数据,然后保存起来。那怎么筛选到咱们需要的数据呢,就得用正则表达式了,正则表达就是写各种规则来匹配咱们想要的数据。

我的文章中有一个分类专门是正则表达式,知识点可能没有完全覆盖到,但是基本的都在了,可以看看

在这再大致梳理下

匹配字符串的几个方法

import re
s='besttest is good'
print(re.match('best',s)) 
#match方法接收3个参数,第一个是匹配的规则,也就是正则表达式,第二个是要查找的字符串,
#第三个参数不是必填的,用于控制正则表达式的匹配方式,看下面正则表达式的匹配模式。是从字符串的第一个单词中匹配字符串,如果匹配到返回一个对象,如果匹配不到,则返回None
#>>><_sre.SRE_Match object; span=(0, 4), match='best'>
print(re.search('best',s))
#search方法的参数和match一样,和match方法不一样的是,match是从字符串里面的第一个单词里面找,而search方法则是从字符串的整个内容里面找,如果找到了就返回第一个,找不到就返回None
#>>> <_sre.SRE_Match object; span=(0, 4), match='best'>
print(re.findall('best',s))
#findall方法的参数上面的match、search一样,和他们不一样的是,findall会返回所有一个list,把所有匹配到的字符串,放到这个list里面,如果找不到的话,就返回一个空的list
#>>> ['best']
 
print(re.sub('best','Best',s))
#sub方法和字符串的replace方法一样,是用来替换字符串的,把匹配到的值替换成一个新的字符串,接收3个参数,第一个是正则表达式,第二个是要替换成什么,第三个就是要查找的字符串,会返回一个新的字符串,如果匹配不到的话,返回原来的字符串
#>>> Besttest is good
print(re.split('best',s))
#split 方法和字符串的split方法一样,是用来分割字符的,按照匹配到的字符串进行分割,返回的是一个list,如果匹配不到的话,那返回的list中还是原来的字符串
#>>> ['', 'test is good']

常用正则表达式符号

1、数量词

'*'     匹配*号前的字符0次或多次,只是*前面的一个字符
print(re.findall(r'be*','besttest very best'))
>>> ['be']
'+'     匹配前一个字符1次或多次,只是+前面的一个字符
print(re.findall(r'st+','besttest is best'))
>>> ['stt', 'st', 'st']
'?'     匹配前一个字符1次或0次,只是?前面的一个字符
print(re.findall(r'st?','besttest is best'))
'{m}'   匹配前一个字符m次
print(re.findall(r't{2}','besttest is best'))
>>> ['tt']
'{n,m}' 匹配前一个字符n到m次
print(re.findall(r't{1,2}','besttest is best'))
>>> ['tt', 't', 't']

  2、一般字符串

'.'     默认匹配除\n之外的任意一个字符
print(re.findall(r'b.','besttest is good'))
'[....]',字符集合,
>>> ['be']
>>> ['st', 'st', 's', 'st']
'\'   转译符,前面的* + ?这样的字符都有特殊含义了,如果你想就想找它的话,那就得转译了
意思就是说如果你想让特殊字符失去以前的含义,那么就得给它前面加上\
print(re.findall(r'\?','besttest is best????'))
>>> ['?', '?', '?', '?']
'|'     匹配|左或|右的字符
print(re.findall(r'best|is','besttest is best'))
>>> ['best', 'is', 'best']
'[]' 字符集合,某些字符的集合,匹配的时候是这个集合里面的任意一个就行
print(re.findall(r'be[stacj]','besttest is best bejson'))
>>>['bes', 'bes', 'bej']
在[]里面如果用^的话代表取反,也就是不包括的这些字符串的
print(re.findall(r'be[^stac]','besttest is best bejson')) 

  3、边界匹配

'^'     匹配以什么字符开头,多行情况下匹配每一行的开头
print(re.findall(r'^b','besttest is good'))
>>> ['b']
print(re.findall(r'^b','besttest is good\nbest',re.M))#多行模式
>>> ['b','b']
'$'     匹配以什么字符结尾,多行情况下匹配每一行的结尾
print(re.findall(r'd$','besttest is good'))
>>> ['d']
print(re.findall(r'd$','besttest is good\nbest is good',re.M<span style="line-height:1.5;">))#多行模式</span> >>>['d','d']
'\A' 仅以什么字符开头,和^不同的是它不能用多行模式
print(re.findall(r'\Ab','besttest is good'))
>>> ['b']
'\Z' 仅以什么字符结尾,和$不同的是它不能用多行模式
print(re.findall(r'd\Z','besttest is good'))
>>> ['d']

  4、预定义字符集合

'\d'  匹配数字0-9
print(re.findall(r'\d+','sdf2342312sdfs'))
>>> ['2342312']
'\D'    匹配非数字
print(re.findall(r'\D','sdf2342312sdfs'))
>>>['sdf', 'sdfs']
'\w'    匹配[A-Za-z0-9],也就是所有的字母和数字
print(re.findall(r'\w','sdf234%^2312sdfs&'))
>>>['sdf234', '2312sdfs']
'\W' 匹配不是[A-Za-z0-9],也就是不是字母和数字
print(re.findall(r'\W','sdf234%^2312sdfs&'))
>>>['%', '^', '&']
'\s' 匹配空白字符、\t、\n、\r,空格
print(re.findall('\s','axss\n\tsdf\t\r\t'))
>>> ['\n', '\t', '\t', '\r', '\t']
'\S'匹配空白字符,不是\t、\n、\r,空格
print(re.findall('\s','axss\n\tsdf\t\r\t'))
>>>['\n', '\t', '\t', '\r', '\t']

  5、分组匹配

'(...)' 分组匹配,把某些规则写成在一个组里,这样就可以直接对这个进行一些匹配了,举个例子的话,如果要匹配ip地址的话
ip地址是类似这样的192.168.5.1,每一位都是1位或者3位的数字然后后面有个点正常写的话,得这么写
print(re.findall(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}',"192.168.1.3"))
>>> ['192.168.1.3']
这样写的话,有点麻烦了,通过上面的我们可以发现规律,除了第一个后面的全都是'.\d{1,3}',写重复的代码就是低级的,这样的话就可以用分组了
就把'.\d{1,3}'当做一个整体,然后让他们出现3次就ok了,可以改成下面这样的
print(re.search(r'\d{1,3}(.\d{1,3}){3}',"192.168.1.3").group())这个是用search方法的,结果和上面的一样的
>>> 192.168.1.3
print(re.findall(r'\d{1,3}(.\d{1,3}){3}',"192.168.1.3"))咱们继续用findall方法,发现结果是下面的
>>> ['.3']
为啥会这样呢,用match方法和search方法都是正常的,findall方法这里有个坑,就是如果findall方法里面有分组的话,那结果就只是分组里面的内容
,如果想让结果正确的话就在分组最前面写上'?:',一个问号和一个冒号就好了,启用“不捕捉模式”
print(re.findall(r'\d{1,3}(?:.\d{1,3}){3}',"192.168.1.3"))
这么写结果就对了  

正则表达式匹配模式

   正则匹配模式是用在match、search、findall里面的第三个参数,还有其他的模式,但是一般也用不到,就这两种能用到,别的就不记了

re.I: #忽略大小写
re.M: #多行模式,改变'^'和'$'的行为
re.findall('pattern', 'string',re.I)
posted @ 2020-04-18 13:21  飞鸟与新月  阅读(237)  评论(0编辑  收藏  举报