常见的爬虫分析库(3)-Python正则表达式与re模块

在线正则表达式测试

http://tool.oschina.net/regex/

常见匹配模式

模式描述
\w匹配字母数字及下划线
\W匹配非字母数字下划线
\s匹配任意空白字符,等价于 [\t\n\r\f].
\S匹配任意非空字符
\d匹配任意数字,等价于 [0-9]
\D匹配任意非数字
\A匹配字符串开始
\Z匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串
\z匹配字符串结束
\G匹配最后匹配完成的位置
\n匹配一个换行符
\t匹配一个制表符
^匹配字符串的开头
$匹配字符串的末尾。
.匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。
[...]用来表示一组字符,单独列出:[amk] 匹配 'a','m'或'k'
[^...]不在[]中的字符:[^abc] 匹配除了a,b,c之外的字符。
*匹配0个或多个的表达式。
+匹配1个或多个的表达式。
?匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式
{n}精确匹配n个前面表达式。
{n, m}匹配 n 到 m 次由前面的正则表达式定义的片段,贪婪方式
a|b匹配a或b
( )匹配括号内的表达式,也表示一个组

re.match

re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。

1
re.match(pattern, string, flags = 0 )

最常规的匹配

1
2
3
4
5
6
7
8
import  re
 
content  =  'Hello 123 4567 World_This is a Regex Demo'
print ( len (content))
result  =  re.match( '^Hello\s\d\d\d\s\d{4}\s\w{10}.*Demo$' , content)
print (result)
print (result.group())
print (result.span())
41
<_sre.SRE_Match object; span=(0, 41), match='Hello 123 4567 World_This is a Regex Demo'>
Hello 123 4567 World_This is a Regex Demo
(0, 41)

泛匹配

1
2
3
4
5
6
7
import  re
 
content  =  'Hello 123 4567 World_This is a Regex Demo'
result  =  re.match( '^Hello.*Demo$' , content)
print (result)
print (result.group())
print (result.span())
<_sre.SRE_Match object; span=(0, 41), match='Hello 123 4567 World_This is a Regex Demo'>
Hello 123 4567 World_This is a Regex Demo
(0, 41)

匹配目标

1
2
3
4
5
6
7
import  re
 
content  =  'Hello 1234567 World_This is a Regex Demo'
result  =  re.match( '^Hello\s(\d+)\sWorld.*Demo$' , content)
print (result)
print (result.group( 1 ))
print (result.span())
<_sre.SRE_Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'>
1234567
(0, 40)

贪婪匹配

1
2
3
4
5
6
import  re
 
content  =  'Hello 1234567 World_This is a Regex Demo'
result  =  re.match( '^He.*(\d+).*Demo$' , content)
print (result)
print (result.group( 1 ))
<_sre.SRE_Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'>
7

非贪婪匹配

1
2
3
4
5
6
import  re
 
content  =  'Hello 1234567 World_This is a Regex Demo'
result  =  re.match( '^He.*?(\d+).*Demo$' , content)
print (result)
print (result.group( 1 ))
<_sre.SRE_Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'>
1234567

匹配模式

1
2
3
4
5
6
7
import  re
 
content  =  '''Hello 1234567 World_This
is a Regex Demo
'''
result  =  re.match( '^He.*?(\d+).*?Demo$' , content, re.S)
print (result.group( 1 ))
1234567

转义

1
2
3
4
5
import  re
 
content  =  'price is $5.00'
result  =  re.match( 'price is $5.00' , content)
print (result)
None
1
2
3
4
5
import  re
 
content  =  'price is $5.00'
result  =  re.match( 'price is \$5\.00' , content)
print (result)
<_sre.SRE_Match object; span=(0, 14), match='price is $5.00'>

总结:尽量使用泛匹配、使用括号得到匹配目标、尽量使用非贪婪模式、有换行符就用re.S

re.search

re.search 扫描整个字符串并返回第一个成功的匹配。

1
2
3
4
5
import  re
 
content  =  'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
result  =  re.match( 'Hello.*?(\d+).*?Demo' , content)
print (result)
None
1
2
3
4
5
6
import  re
 
content  =  'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
result  =  re.search( 'Hello.*?(\d+).*?Demo' , content)
print (result)
print (result.group( 1 ))
<_sre.SRE_Match object; span=(13, 53), match='Hello 1234567 World_This is a Regex Demo'>
1234567

总结:为匹配方便,能用search就不用match

匹配演练

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import  re
 
html  =  '''<div id="songs-list">
     <h2 class="title">经典老歌</h2>
     <p class="introduction">
         经典老歌列表
     </p>
     <ul id="list" class="list-group">
         <li data-view="2">一路上有你</li>
         <li data-view="7">
             <a href="/2.mp3" singer="任贤齐">沧海一声笑</a>
         </li>
         <li data-view="4" class="active">
             <a href="/3.mp3" singer="齐秦">往事随风</a>
         </li>
         <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li>
         <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li>
         <li data-view="5">
             <a href="/6.mp3" singer="邓丽君"><i class="fa fa-user"></i>但愿人长久</a>
         </li>
     </ul>
</div>'''
result  =  re.search( '<li.*?active.*?singer="(.*?)">(.*?)</a>' , html, re.S)
if  result:
     print (result.group( 1 ), result.group( 2 ))
齐秦 往事随风
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import  re
 
html  =  '''<div id="songs-list">
     <h2 class="title">经典老歌</h2>
     <p class="introduction">
         经典老歌列表
     </p>
     <ul id="list" class="list-group">
         <li data-view="2">一路上有你</li>
         <li data-view="7">
             <a href="/2.mp3" singer="任贤齐">沧海一声笑</a>
         </li>
         <li data-view="4" class="active">
             <a href="/3.mp3" singer="齐秦">往事随风</a>
         </li>
         <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li>
         <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li>
         <li data-view="5">
             <a href="/6.mp3" singer="邓丽君">但愿人长久</a>
         </li>
     </ul>
</div>'''
result  =  re.search( '<li.*?singer="(.*?)">(.*?)</a>' , html, re.S)
if  result:
     print (result.group( 1 ), result.group( 2 ))
任贤齐 沧海一声笑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import  re
 
html  =  '''<div id="songs-list">
     <h2 class="title">经典老歌</h2>
     <p class="introduction">
         经典老歌列表
     </p>
     <ul id="list" class="list-group">
         <li data-view="2">一路上有你</li>
         <li data-view="7">
             <a href="/2.mp3" singer="任贤齐">沧海一声笑</a>
         </li>
         <li data-view="4" class="active">
             <a href="/3.mp3" singer="齐秦">往事随风</a>
         </li>
         <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li>
         <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li>
         <li data-view="5">
             <a href="/6.mp3" singer="邓丽君">但愿人长久</a>
         </li>
     </ul>
</div>'''
result  =  re.search( '<li.*?singer="(.*?)">(.*?)</a>' , html)
if  result:
     print (result.group( 1 ), result.group( 2 ))
beyond 光辉岁月

re.findall

搜索字符串,以列表形式返回全部能匹配的子串。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import  re
 
html  =  '''<div id="songs-list">
     <h2 class="title">经典老歌</h2>
     <p class="introduction">
         经典老歌列表
     </p>
     <ul id="list" class="list-group">
         <li data-view="2">一路上有你</li>
         <li data-view="7">
             <a href="/2.mp3" singer="任贤齐">沧海一声笑</a>
         </li>
         <li data-view="4" class="active">
             <a href="/3.mp3" singer="齐秦">往事随风</a>
         </li>
         <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li>
         <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li>
         <li data-view="5">
             <a href="/6.mp3" singer="邓丽君">但愿人长久</a>
         </li>
     </ul>
</div>'''
results  =  re.findall( '<li.*?href="(.*?)".*?singer="(.*?)">(.*?)</a>' , html, re.S)
print (results)
print ( type (results))
for  result  in  results:
     print (result)
     print (result[ 0 ], result[ 1 ], result[ 2 ])
  output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import  re
 
html  =  '''<div id="songs-list">
     <h2 class="title">经典老歌</h2>
     <p class="introduction">
         经典老歌列表
     </p>
     <ul id="list" class="list-group">
         <li data-view="2">一路上有你</li>
         <li data-view="7">
             <a href="/2.mp3" singer="任贤齐">沧海一声笑</a>
         </li>
         <li data-view="4" class="active">
             <a href="/3.mp3" singer="齐秦">往事随风</a>
         </li>
         <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li>
         <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li>
         <li data-view="5">
             <a href="/6.mp3" singer="邓丽君">但愿人长久</a>
         </li>
     </ul>
</div>'''
results  =  re.findall( '<li.*?>\s*?(<a.*?>)?(\w+)(</a>)?\s*?</li>' , html, re.S)
print (results)
for  result  in  results:
     print (result[ 1 ])
  output

re.sub

替换字符串中每一个匹配的子串后返回替换后的字符串。

1
2
3
4
5
import  re
 
content  =  'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
content  =  re.sub( '\d+' , '', content)
print (content)
Extra stings Hello  World_This is a Regex Demo Extra stings
1
2
3
4
5
import  re
 
content  =  'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
content  =  re.sub( '\d+' 'Replacement' , content)
print (content)
Extra stings Hello Replacement World_This is a Regex Demo Extra stings
1
2
3
4
5
import  re
 
content  =  'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
content  =  re.sub( '(\d+)' , r '\1 8910' , content)
print (content)
Extra stings Hello 1234567 8910 World_This is a Regex Demo Extra stings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import  re
 
html  =  '''<div id="songs-list">
     <h2 class="title">经典老歌</h2>
     <p class="introduction">
         经典老歌列表
     </p>
     <ul id="list" class="list-group">
         <li data-view="2">一路上有你</li>
         <li data-view="7">
             <a href="/2.mp3" singer="任贤齐">沧海一声笑</a>
         </li>
         <li data-view="4" class="active">
             <a href="/3.mp3" singer="齐秦">往事随风</a>
         </li>
         <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li>
         <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li>
         <li data-view="5">
             <a href="/6.mp3" singer="邓丽君">但愿人长久</a>
         </li>
     </ul>
</div>'''
html  =  re.sub( '<a.*?>|</a>' , '', html)
print (html)
results  =  re.findall( '<li.*?>(.*?)</li>' , html, re.S)
print (results)
for  result  in  results:
     print (result.strip())
  output

re.compile

将正则字符串编译成正则表达式对象

将一个正则表达式串编译成正则对象,以便于复用该匹配模式:

1
2
3
4
5
6
7
8
import  re
 
content  =  '''Hello 1234567 World_This
is a Regex Demo'''
pattern  =  re. compile ( 'Hello.*Demo' , re.S)
result  =  re.match(pattern, content)
#result = re.match('Hello.*Demo', content, re.S)
print (result)
<_sre.SRE_Match object; span=(0, 40), match='Hello 1234567 World_This\nis a Regex Demo'>

实战练习

源码在git上:https://gitee.com/longlovemeng/Spider.git

转载于:https://www.cnblogs.com/yunlongaimeng/p/9802104.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值