Python爬蟲之正則表達(dá)式的使用教程詳解
正則表達(dá)式的使用
re.match(pattern,string,flags=0)
re.match嘗試從字符串的起始位置匹配一個(gè)模式,如果不是起始位置匹配成功的話,match()就返回none
參數(shù)介紹:
pattern:正則表達(dá)式
string:匹配的目標(biāo)字符串
flags:匹配模式
正則表達(dá)式的匹配模式:
最常規(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é)果運(yùn)行如下:
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é)果運(yùn)行如下:
<_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)
在正則表達(dá)式中使用()將要獲取的內(nèi)容括起來(lá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é)果運(yùn)行如下:
<_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))
注意:.*會(huì)盡可能的多匹配字符
非貪婪匹配
import re content ='hello 123456 World_This is a Regex Demo' result = re.match('^he.*?(\d+).*Demo$',content) print(result) print(result.group(1))
注意:.*?會(huì)盡可能匹配少的字符
使用匹配模式
在解析HTML代碼時(shí)會(huì)有換行,這時(shí)我們就要使用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))
運(yùn)行結(jié)果如下:
<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>
123456
轉(zhuǎn)義
在解析過(guò)程中遇到特殊字符,就需要做轉(zhuǎn)義,比如下面的$符號(hào)。
import re content = 'price is $5.00' result = re.match('^price.*\$5\.00',content) print(result.group())
總結(jié):盡量使用泛匹配,使用括號(hào)得到匹配目標(biāo),盡量使用非貪婪模式,有換行就用re.S
re.search(pattern,string,flags=0)
re.search掃描整個(gè)字符串并返回第一個(gè)成功的匹配。
比如我想要提取字符串中的123456,使用match方法無(wú)法提取,只能使用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())
運(yùn)行結(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共有五個(gè)參數(shù)
三個(gè)必選參數(shù) pattern,repl,string
兩個(gè)可選參數(shù)count,flags
替換字符串中每一個(gè)匹配的字符串后替換后的字符串
import re content = 'hello 123456 World_This is a Regex Demo' content = re.sub('\d+','',content) print(content)
運(yùn)行結(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)
運(yùn)行結(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)
運(yùn)行結(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)
運(yùn)行結(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ù)包含的正則表達(dá)式的字符串創(chuàng)建模式對(duì)象??梢詫?shí)現(xiàn)更有效率的匹配
將正則表達(dá)式編譯成正則表達(dá)式對(duì)象,以便于復(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())
運(yùn)行結(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="https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2532008868.jpg" alt="鐮倉(cāng)物語(yǔ)" data-x="2143" data-y="2993"> </div> <p> 鐮倉(cāng)物語(yǔ) <strong>6.9</strong> </p> </a> <a class="item" target="_blank" > <div class="cover-wp" data-isnew="false" data-id="26683421"> <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2528281606.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="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2521583093.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="https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2528842218.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="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530872223.jpg" alt="風(fēng)語(yǔ)咒" data-x="1079" data-y="1685"> </div> <p> 風(fēng)語(yǔ)咒 <strong>6.9</strong> </p> </a> <a class="item" target="_blank" > <div class="cover-wp" data-isnew="false" data-id="26630714"> <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530591543.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="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2526405034.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="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2527484082.jpg" alt="摩天營(yíng)救" data-x="1371" data-y="1920"> </div> <p> 摩天營(yíng)救 <strong>6.4</strong> </p> </a> <a class="item" target="_blank" > <div class="cover-wp" data-isnew="false" data-id="24773958"> <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2517753454.jpg" alt="復(fù)仇者聯(lián)盟3:無(wú)限戰(zhàn)爭(zhēng)" data-x="1968" data-y="2756"> </div> <p> 復(fù)仇者聯(lián)盟3:無(wú)限戰(zhàn)爭(zhēng) <strong>8.1</strong> </p> </a> </div> ''' count = 0 for list in result: print(result[count]) count+=1
運(yùn)行結(jié)果如下:
('解除好友2:暗網(wǎng)', '7.9')
('鐮倉(cāng)物語(yǔ)', '6.9')
('特工', '8.3')
('幸福的拉扎羅', '8.6')
('大師兄', '5.2')
('風(fēng)語(yǔ)咒', '6.9')
('精靈旅社3:瘋狂假期', '6.8')
('狄仁杰之四大天王', '6.2')
('摩天營(yíng)救', '6.4')
('復(fù)仇者聯(lián)盟3:無(wú)限戰(zhàn)爭(zhēng)', '8.1')
總結(jié)
以上所述是小編給大家介紹的Python爬蟲之正則表達(dá)式的使用教程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 零基礎(chǔ)寫python爬蟲之神器正則表達(dá)式
- Python的爬蟲包Beautiful Soup中用正則表達(dá)式來(lái)搜索
- python3爬蟲之入門基礎(chǔ)和正則表達(dá)式
- python爬蟲 正則表達(dá)式使用技巧及爬取個(gè)人博客的實(shí)例講解
- python爬蟲正則表達(dá)式之處理?yè)Q行符
- 玩轉(zhuǎn)python爬蟲之正則表達(dá)式
- Python爬蟲正則表達(dá)式常用符號(hào)和方法
- Python爬蟲之正則表達(dá)式基本用法實(shí)例分析
- Python 爬蟲學(xué)習(xí)筆記之正則表達(dá)式
- Python爬蟲教程之利用正則表達(dá)式匹配網(wǎng)頁(yè)內(nèi)容
相關(guān)文章
python解決漢字編碼問(wèn)題:Unicode Decode Error
最近在利用python讀取一個(gè)含有漢字的文檔時(shí)導(dǎo)致出現(xiàn)了亂碼,并報(bào)出了兩個(gè)錯(cuò)誤,無(wú)奈只能上網(wǎng)找尋答案,后通過(guò)網(wǎng)友的幫助解決了這個(gè)問(wèn)題,想著總結(jié)一下,下面這篇文章就主要介紹了python如何解決漢字編碼問(wèn)題,有需要的朋友們可以參考借鑒。2017-01-01python實(shí)現(xiàn)半自動(dòng)化發(fā)送微信信息
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)半自動(dòng)化發(fā)送微信信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08深入解析神經(jīng)網(wǎng)絡(luò)從原理到實(shí)現(xiàn)
這篇文章主要介紹了深入解析神經(jīng)網(wǎng)絡(luò)從原理到實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python進(jìn)程管理工具supervisor的安裝與使用教程
supervisor是用python寫的一個(gè)進(jìn)程管理工具,用來(lái)啟動(dòng),重啟,關(guān)閉進(jìn)程。下面這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)的進(jìn)程管理工具supervisor的安裝與使用的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09Win7上搭建Cocos2d-x 3.1.1開發(fā)環(huán)境
現(xiàn)在,越來(lái)越多的公司采用Cocos2d-x 3.0來(lái)開發(fā)游戲了,但是現(xiàn)在這樣的文章并不多,所以打算寫一系列來(lái)幫助初學(xué)者快速掌握Cocos2d-x 3.0。首先就從開發(fā)環(huán)境的大家說(shuō)起吧2014-07-07python中矩陣合并、拼接、組合的實(shí)現(xiàn)示例
本文主要介紹了python中矩陣合并、拼接、組合的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07詳談python read readline readlines的區(qū)別
下面小編就為大家?guī)?lái)一篇詳談python read readline readlines的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09Python selenium使用autoIT上傳附件過(guò)程詳解
這篇文章主要介紹了Python selenium使用autoIT上傳附件過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python中日期和時(shí)間的用法超強(qiáng)總結(jié)
時(shí)間無(wú)疑是生活各個(gè)方面中最關(guān)鍵的因素之一,因此,記錄和跟蹤時(shí)間變得非常重要。在?Python?中,可以通過(guò)其內(nèi)置庫(kù)跟蹤日期和時(shí)間。今天我們來(lái)介紹關(guān)于?Python?中的日期和時(shí)間,一起來(lái)了解如何使用time和datetime模塊查找和修改日期和時(shí)間2022-10-10