bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

使用正則表達式怎么實現一個Python爬蟲-創新互聯

今天就跟大家聊聊有關使用正則表達式怎么實現一個Python爬蟲,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

創新互聯成立于2013年,先為睢陽等服務建站,睢陽等地企業,進行企業商務咨詢服務。為睢陽企業網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。

正則表達式的使用

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

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

參數介紹:

pattern:正則表達式

string:匹配的目標字符串

flags:匹配模式

正則表達式的匹配模式:

使用正則表達式怎么實現一個Python爬蟲

最常規的匹配

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()) #返回匹配結果
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 re
content ='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 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))#獲取匹配目標

結果運行如下:

<_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))

運行結果如下:

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

轉義

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

import re
content = '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 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())

運行結果如下:

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

匹配演練

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

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="網絡歌曲" 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">網絡歌曲</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
網絡歌曲
2009年中信出版社出版圖書
re.sub( pattern,repl,string,count,flags)

re.sub共有五個參數

三個必選參數 pattern,repl,string

兩個可選參數count,flags

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

import re
content = '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 re
html ='''
<li>
<a title="網絡歌曲" 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">網絡歌曲</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)

運行結果如下,里面就沒有li標簽

<a title="網絡歌曲" 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">網絡歌曲</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])

該函數根據包含的正則表達式的字符串創建模式對象。可以實現更有效率的匹配

將正則表達式編譯成正則表達式對象,以便于復用該匹配模式

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()) 

運行結果如下:

hello 123456 World_This is a Regex Demo

綜合使用

import re
html = '''
<div class="slide-page"  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" alt="解除好友2:暗網" data-x="694" data-y="1000">
      </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" alt="鐮倉物語" data-x="2143" data-y="2993">
      </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" alt="特工" data-x="690" data-y="986">
      </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" alt="幸福的拉扎羅" data-x="640" data-y="914">
      </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" alt="大師兄" data-x="679" data-y="950">
      </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" alt="風語咒" data-x="1079" data-y="1685">
      </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" alt="精靈旅社3:瘋狂假期" data-x="1063" data-y="1488">
      </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" alt="狄仁杰之四大天王" data-x="2500" data-y="3500">
      </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" alt="摩天營救" data-x="1371" data-y="1920">
      </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" alt="復仇者聯盟3:無限戰爭" data-x="1968" data-y="2756">
      </div>
      <p>
        復仇者聯盟3:無限戰爭
          <strong>8.1</strong>
      </p>
    </a>
  </div>
'''
count = 0
for 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爬蟲有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創新互聯成都網站設計公司行業資訊頻道,感謝大家的支持。

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

本文名稱:使用正則表達式怎么實現一個Python爬蟲-創新互聯
分享網址:http://vcdvsql.cn/article38/cdgopp.html

成都網站建設公司_創新互聯,為您提供軟件開發做網站手機網站建設服務器托管網站設計公司品牌網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都定制網站建設