国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python爬蟲之正則表達式的使用教程詳解

2020-01-04 14:14:29
字體:
來源:轉載
供稿:網友

正則表達式的使用

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

re.match嘗試從字符串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就返回none

參數介紹:

pattern:正則表達式

string:匹配的目標字符串

flags:匹配模式

正則表達式的匹配模式:

Python,爬蟲,正則表達式

最常規的匹配

import recontent ='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()) #返回匹配結果print(result.span()) #返回匹配結果的范圍

結果運行如下:

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)

泛匹配

使用(.*)匹配更多內容

import recontent ='hello 123456 World_This is a Regex Demo'result = re.match('^hello.*Demo$',content)print(result)print(result.group())

 

結果運行如下:

<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>
hello 123456 World_This is a Regex Demo

匹配目標

在正則表達式中使用()將要獲取的內容括起來

使用group(1)獲取第一處,group(2)獲取第二處,如此可以提取我們想要獲取的內容

import recontent ='hello 123456 World_This is a Regex Demo'result = re.match('^hello/s(/d{6})/s.*Demo$',content)print(result)print(result.group(1))#獲取匹配目標

結果運行如下:

<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>
123456

貪婪匹配

import recontent ='hello 123456 World_This is a Regex Demo'result = re.match('^he.*(/d+).*Demo$',content)print(result)print(result.group(1))

注意:.*會盡可能的多匹配字符

非貪婪匹配

import recontent ='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 recontent ='hello 123456 World_This ' /'is a Regex Demo'result = re.match('^he.*?(/d+).*?Demo$',content,re.S)print(result)print(result.group(1))

運行結果如下:

<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>
123456

轉義

在解析過程中遇到特殊字符,就需要做轉義,比如下面的$符號。

import recontent = 'price is $5.00'result = re.match('^price.*/$5/.00',content)print(result.group())

總結:盡量使用泛匹配,使用括號得到匹配目標,盡量使用非貪婪模式,有換行就用re.S

re.search(pattern,string,flags=0)

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

比如我想要提取字符串中的123456,使用match方法無法提取,只能使用search方法。

import recontent ='hello 123456 World_This is a Regex Demo'result = re.match('/d{6}',content)print(result)import recontent ='hello 123456 World_This is a Regex Demo'result = re.search('/d{6}',content)print(result)print(result.group())

運行結果如下:

<_sre.SRE_Match object; span=(6, 12), match='123456'>

匹配演練

可以匹配代碼里結構相同的部分,這樣可以返回你需要的內容

import recontent = '<a style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";">	
import rehtml ='''<li><a  print(result[count])  count+=1網絡歌曲2009年中信出版社出版圖書re.sub( pattern,repl,string,count,flags)

re.sub共有五個參數

三個必選參數 pattern,repl,string

兩個可選參數count,flags

替換字符串中每一個匹配的字符串后替換后的字符串

import recontent = 'hello 123456 World_This is a Regex Demo'content = re.sub('/d+','',content)print(content)

運行結果如下:

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)

運行結果如下:

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)

運行結果如下:

hello 123456 789 World_This is a Regex Demo

注意:這里/1代表前面匹配的123456

演練

在這里我們替換li標簽

import rehtml ='''<li><a style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";">	
<a style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";">	
import recontent = 'hello 123456 ' /'World_This is a Regex Demo'pattern = re.compile('hello.*?Demo',re.S)result = re.match(pattern,content)print(result.group()) 

運行結果如下:

hello 123456 World_This is a Regex Demo

綜合使用

import rehtml = '''<div class="slide-page" style="width: 700px;" data-index="1">    <a class="item" target="_blank" href="https://movie.douban.com/subject/26725678/?tag=熱門&from=gaia">      <div class="cover-wp" data-isnew="false" data-id="26725678">        <img src="https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2525020357.jpg"      </div>      <p>        解除好友2:暗網          <strong>7.9</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/26916229/?tag=熱門&from=gaia_video">      <div class="cover-wp" data-isnew="false" data-id="26916229">        <img src="https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2532008868.jpg"      </div>      <p>        鐮倉物語          <strong>6.9</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/26683421/?tag=熱門&from=gaia">      <div class="cover-wp" data-isnew="false" data-id="26683421">        <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2528281606.jpg"      </div>      <p>        特工          <strong>8.3</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/27072795/?tag=熱門&from=gaia">      <div class="cover-wp" data-isnew="false" data-id="27072795">        <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2521583093.jpg"      </div>      <p>        幸福的拉扎羅          <strong>8.6</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/27201353/?tag=熱門&from=gaia_video">      <div class="cover-wp" data-isnew="false" data-id="27201353">        <img src="https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2528842218.jpg"      </div>      <p>        大師兄          <strong>5.2</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/30146756/?tag=熱門&from=gaia_video">      <div class="cover-wp" data-isnew="false" data-id="30146756">        <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530872223.jpg"      </div>      <p>        風語咒          <strong>6.9</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/26630714/?tag=熱門&from=gaia">      <div class="cover-wp" data-isnew="false" data-id="26630714">        <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530591543.jpg"      </div>      <p>        精靈旅社3:瘋狂假期          <strong>6.8</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/25882296/?tag=熱門&from=gaia_video">      <div class="cover-wp" data-isnew="false" data-id="25882296">        <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2526405034.jpg"      </div>      <p>        狄仁杰之四大天王          <strong>6.2</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/26804147/?tag=熱門&from=gaia_video">      <div class="cover-wp" data-isnew="false" data-id="26804147">        <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2527484082.jpg"      </div>      <p>        摩天營救          <strong>6.4</strong>      </p>    </a>    <a class="item" target="_blank" href="https://movie.douban.com/subject/24773958/?tag=熱門&from=gaia_video">      <div class="cover-wp" data-isnew="false" data-id="24773958">        <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2517753454.jpg"      </div>      <p>        復仇者聯盟3:無限戰爭          <strong>8.1</strong>      </p>    </a>  </div>'''count = 0for list in result:  print(result[count])  count+=1

運行結果如下:

('解除好友2:暗網', '7.9')
('鐮倉物語', '6.9')
('特工', '8.3')
('幸福的拉扎羅', '8.6')
('大師兄', '5.2')
('風語咒', '6.9')
('精靈旅社3:瘋狂假期', '6.8')
('狄仁杰之四大天王', '6.2')
('摩天營救', '6.4')
('復仇者聯盟3:無限戰爭', '8.1')

總結

以上所述是小編給大家介紹的Python爬蟲之正則表達式的使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湛江市| 夏河县| 红原县| 邵阳县| 临清市| 德安县| 榆中县| 临洮县| 甘孜县| 浪卡子县| 公主岭市| 吴桥县| 望谟县| 乌拉特后旗| 托克托县| 齐齐哈尔市| 德州市| 从江县| 肃北| 芦溪县| 临澧县| 南陵县| 枣阳市| 昂仁县| 道孚县| 招远市| 宜丰县| 勃利县| 班戈县| 平乐县| 兴宁市| 赤壁市| 盈江县| 周宁县| 东乌珠穆沁旗| 漠河县| 文昌市| 南京市| 灵山县| 湖北省| 惠来县|