Python爬蟲之正則表達式的使用教程詳解
正則表達式的使用
re.match(pattern,string,flags=0)
re.match嘗試從字符串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就返回none
參數(shù)介紹:
pattern:正則表達式
string:匹配的目標(biāo)字符串
flags:匹配模式
正則表達式的匹配模式:
最常規(guī)的匹配
import re
content ='hello 123456 World_This is a Regex Demo'
print(len(content))
result = re.match('^hello\s\d{6}\s\w{10}.*Demo$$',content)
print(result)
print(result.group()) #返回匹配結(jié)果
print(result.span()) #返回匹配結(jié)果的范圍
結(jié)果運行如下:
39
<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>
hello 123456 World_This is a Regex Demo
(0, 39)
泛匹配
使用(.*)匹配更多內(nèi)容
import re
content ='hello 123456 World_This is a Regex Demo'
result = re.match('^hello.*Demo$',content)
print(result)
print(result.group())
結(jié)果運行如下:
<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>
hello 123456 World_This is a Regex Demo
匹配目標(biāo)
在正則表達式中使用()將要獲取的內(nèi)容括起來
使用group(1)獲取第一處,group(2)獲取第二處,如此可以提取我們想要獲取的內(nèi)容
import re
content ='hello 123456 World_This is a Regex Demo'
result = re.match('^hello\s(\d{6})\s.*Demo$',content)
print(result)
print(result.group(1))#獲取匹配目標(biāo)
結(jié)果運行如下:
<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>
123456
貪婪匹配
import re
content ='hello 123456 World_This is a Regex Demo'
result = re.match('^he.*(\d+).*Demo$',content)
print(result)
print(result.group(1))
注意:.*會盡可能的多匹配字符
非貪婪匹配
import re
content ='hello 123456 World_This is a Regex Demo'
result = re.match('^he.*?(\d+).*Demo$',content)
print(result)
print(result.group(1))
注意:.*?會盡可能匹配少的字符
使用匹配模式
在解析HTML代碼時會有換行,這時我們就要使用re.S
import re
content ='hello 123456 World_This ' \
'is a Regex Demo'
result = re.match('^he.*?(\d+).*?Demo$',content,re.S)
print(result)
print(result.group(1))
運行結(jié)果如下:
<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>
123456
轉(zhuǎn)義
在解析過程中遇到特殊字符,就需要做轉(zhuǎn)義,比如下面的$符號。
import re
content = 'price is $5.00'
result = re.match('^price.*\$5\.00',content)
print(result.group())
總結(jié):盡量使用泛匹配,使用括號得到匹配目標(biāo),盡量使用非貪婪模式,有換行就用re.S
re.search(pattern,string,flags=0)
re.search掃描整個字符串并返回第一個成功的匹配。
比如我想要提取字符串中的123456,使用match方法無法提取,只能使用search方法。
import re
content ='hello 123456 World_This is a Regex Demo'
result = re.match('\d{6}',content)
print(result)
import re
content ='hello 123456 World_This is a Regex Demo'
result = re.search('\d{6}',content)
print(result)
print(result.group())
運行結(jié)果如下:
<_sre.SRE_Match object; span=(6, 12), match='123456'>
匹配演練
可以匹配代碼里結(jié)構(gòu)相同的部分,這樣可以返回你需要的內(nèi)容
import re
content = '<a title="2009年中信出版社出版圖書" href="/doc/2703035-2853985.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank" data-log="old:2703035-2853885,new:2703035-2853985" data-cid="sense-list">2009年中信出版社出版圖書</a>'
result = re.search('<a.*?new:\d{7}-\d{7}.*?>(.*?)</a>',content)
print(result.group(1))
2009年中信出版社出版圖書
re.findall(pattern,string,flags=0)
搜索字符串,以列表形式返回全部能匹配的字串
import re
html ='''
<li>
<a title="網(wǎng)絡(luò)歌曲" href="/doc/2703035-2853927.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank" data-log="old:2703035-2853885,new:2703035-2853927" data-cid="sense-list">網(wǎng)絡(luò)歌曲</a>
</li>
<li>
<a title="2009年中信出版社出版圖書" href="/doc/2703035-2853985.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank" data-log="old:2703035-2853885,new:2703035-2853985" data-cid="sense-list">2009年中信出版社出版圖書</a>
</li>
'''
result = re.findall('<a.*?new:\d{7}-\d{7}.*?>(.*?)</a>',html,re.S)
count = 0
for list in result:
print(result[count])
count+=1
網(wǎng)絡(luò)歌曲
2009年中信出版社出版圖書
re.sub( pattern,repl,string,count,flags)
re.sub共有五個參數(shù)
三個必選參數(shù) pattern,repl,string
兩個可選參數(shù)count,flags
替換字符串中每一個匹配的字符串后替換后的字符串
import re
content = 'hello 123456 World_This is a Regex Demo'
content = re.sub('\d+','',content)
print(content)
運行結(jié)果如下:
hello World_This is a Regex Demo
import re
content = 'hello 123456 World_This is a Regex Demo'
content = re.sub('\d+','what',content)
print(content)
運行結(jié)果如下:
hello what World_This is a Regex Demo
import re
content = 'hello 123456 World_This is a Regex Demo'
content = re.sub('(\d+)',r'\1 789',content)
print(content)
運行結(jié)果如下:
hello 123456 789 World_This is a Regex Demo
注意:這里\1代表前面匹配的123456
演練
在這里我們替換li標(biāo)簽
import re
html ='''
<li>
<a title="網(wǎng)絡(luò)歌曲" href="/doc/2703035-2853927.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank" data-log="old:2703035-2853885,new:2703035-2853927" data-cid="sense-list">網(wǎng)絡(luò)歌曲</a>
</li>
<li>
<a title="2009年中信出版社出版圖書" href="/doc/2703035-2853985.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank" data-log="old:2703035-2853885,new:2703035-2853985" data-cid="sense-list">2009年中信出版社出版圖書</a>
</li>
'''
html = re.sub('<li>|</li>','',html)
print(html)
運行結(jié)果如下,里面就沒有l(wèi)i標(biāo)簽
<a title="網(wǎng)絡(luò)歌曲" href="/doc/2703035-2853927.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank" data-log="old:2703035-2853885,new:2703035-2853927" data-cid="sense-list">網(wǎng)絡(luò)歌曲</a> <a title="2009年中信出版社出版圖書" href="/doc/2703035-2853985.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank" data-log="old:2703035-2853885,new:2703035-2853985" data-cid="sense-list">2009年中信出版社出版圖書</a> compile(pattern [, flags])
該函數(shù)根據(jù)包含的正則表達式的字符串創(chuàng)建模式對象??梢詫崿F(xiàn)更有效率的匹配
將正則表達式編譯成正則表達式對象,以便于復(fù)用該匹配模式
import re
content = 'hello 123456 ' \
'World_This is a Regex Demo'
pattern = re.compile('hello.*?Demo',re.S)
result = re.match(pattern,content)
print(result.group())
運行結(jié)果如下:
hello 123456 World_This is a Regex Demo
綜合使用
import re
html = '''
<div class="slide-page" style="width: 700px;" data-index="1">
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="26725678">
<img src="https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2525020357.jpg" alt="解除好友2:暗網(wǎng)" data-x="694" data-y="1000">
</div>
<p>
解除好友2:暗網(wǎng)
<strong>7.9</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="26916229">
<img src="/a1/uploads/allimg/200111/1006236020-1.jpg" alt="鐮倉物語" data-x="2143" data-y="2993">
</div>
<p>
鐮倉物語
<strong>6.9</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="26683421">
<img src="/a1/uploads/allimg/200111/1006232349-2.jpg" alt="特工" data-x="690" data-y="986">
</div>
<p>
特工
<strong>8.3</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="27072795">
<img src="/a1/uploads/allimg/200111/100623J49-3.jpg" alt="幸福的拉扎羅" data-x="640" data-y="914">
</div>
<p>
幸福的拉扎羅
<strong>8.6</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="27201353">
<img src="/a1/uploads/allimg/200111/100623O24-4.jpg" alt="大師兄" data-x="679" data-y="950">
</div>
<p>
大師兄
<strong>5.2</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="30146756">
<img src="/a1/uploads/allimg/200111/1006234421-5.jpg" alt="風(fēng)語咒" data-x="1079" data-y="1685">
</div>
<p>
風(fēng)語咒
<strong>6.9</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="26630714">
<img src="/a1/uploads/allimg/200111/10062334W-6.jpg" alt="精靈旅社3:瘋狂假期" data-x="1063" data-y="1488">
</div>
<p>
精靈旅社3:瘋狂假期
<strong>6.8</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="25882296">
<img src="/a1/uploads/allimg/200111/100623C00-7.jpg" alt="狄仁杰之四大天王" data-x="2500" data-y="3500">
</div>
<p>
狄仁杰之四大天王
<strong>6.2</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="26804147">
<img src="/a1/uploads/allimg/200111/100623L36-8.jpg" alt="摩天營救" data-x="1371" data-y="1920">
</div>
<p>
摩天營救
<strong>6.4</strong>
</p>
</a>
<a class="item" target="_blank" >
<div class="cover-wp" data-isnew="false" data-id="24773958">
<img src="/a1/uploads/allimg/200111/10062364M-9.jpg" alt="復(fù)仇者聯(lián)盟3:無限戰(zhàn)爭" data-x="1968" data-y="2756">
</div>
<p>
復(fù)仇者聯(lián)盟3:無限戰(zhàn)爭
<strong>8.1</strong>
</p>
</a>
</div>
'''
count = 0
for list in result:
print(result[count])
count+=1
運行結(jié)果如下:
('解除好友2:暗網(wǎng)', '7.9')
('鐮倉物語', '6.9')
('特工', '8.3')
('幸福的拉扎羅', '8.6')
('大師兄', '5.2')
('風(fēng)語咒', '6.9')
('精靈旅社3:瘋狂假期', '6.8')
('狄仁杰之四大天王', '6.2')
('摩天營救', '6.4')
('復(fù)仇者聯(lián)盟3:無限戰(zhàn)爭', '8.1')
總結(jié)
以上所述是小編給大家介紹的Python爬蟲之正則表達式的使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
上一篇:解決正則表達式\w和\d的疑惑
欄 目:正則表達式
下一篇:使用正則表達式實現(xiàn)網(wǎng)頁爬蟲的思路詳解
本文標(biāo)題:Python爬蟲之正則表達式的使用教程詳解
本文地址:http://www.jygsgssxh.com/a1/zhengzebiaodashi/11191.html
您可能感興趣的文章
- 01-11python 用正則表達式篩選文本信息的實例
- 01-11正則表達式之匹配數(shù)字范圍
- 01-11python爬蟲正則表達式之處理換行符
- 01-11python正則表達式之對號入座篇
- 01-11Python爬蟲之正則表達式基本用法實例分析
- 01-11Python正則表達式中的re.S的作用詳解
- 01-11詳解Python中的正則表達式
- 01-11Python實現(xiàn)正則表達式匹配任意的郵箱方法
- 01-11一篇文章搞懂JavaScript正則表達式之方法
- 01-11淺談Python采集網(wǎng)頁時正則表達式匹配換行符的問題


閱讀排行
本欄相關(guān)
- 01-11正則表達式實現(xiàn)添加、刪除、替換三
- 01-11正則表達式之匹配數(shù)字范圍
- 01-11python 用正則表達式篩選文本信息的實
- 01-11正則表達式匹配路由的實現(xiàn)代碼
- 01-11python爬蟲正則表達式之處理換行符
- 01-11js正則表達式 匹配兩個特定字符間的
- 01-11正則表達式截取身份證號碼加密的方
- 01-11python正則表達式之對號入座篇
- 01-11MySQL使用正則表達式進行查詢操作經(jīng)典
- 01-11詳解正則表達式實現(xiàn)二代身份證號碼
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載


