利用python爬虫(part3)--正则表达式
學習筆記
文章目錄
- 正則表達式
- re模塊的使用
- 正則表達式元字符
- 貪婪匹配和非貪婪匹配
- 貪婪模式
- 非貪婪模式
- 正則表達式分組
正則表達式
re模塊的使用
re模塊在爬蟲中常用的方法:
re.findall() re.compile() pattern.finall()匹配方式1(re.findall)
list01=re.findall('正則表達式',html,re.S) #html就是我們獲取的網頁源代碼字符串 #re.S為flag擴展參數,re.S表示使【.】可以匹配換行。這樣【.】就可以匹配任意字符了 #返回值為包含符合正則表達式的字符串的列表關于flag擴展參數,詳細請見正則表達式教程。
匹配方式2(re.compile與pattern.findall)
#創建正則表達式對象 pattern = re.compile('正則表達式',re.S) list02 = pattern.findall(html)舉個例子
代碼:
import res01 = 'abchfgasca\nc' list01 = re.findall('a.c', s01, re.S) print(list01) print('-'*15)pattern = re.compile('a.c') list02 = pattern.findall(s01) print(list02)結果:
['abc', 'asc', 'a\nc'] --------------- ['abc', 'asc']正則表達式元字符
元字符的詳細知識點,具體看正則表達式教程。
| . | 匹配任意一個字符(不包括\n) |
| \d | 匹配任意數字 |
| \s | 匹配空白字符 |
| \S | 匹配非空白字符 |
| * | 出現0次或多次 |
| + | 出現1次或多次 |
| ? | 出現0次或1次 |
| {m,n} | 出現m-n次 |
貪婪匹配和非貪婪匹配
貪婪和非貪婪的詳細知識點,具體看正則表達式教程。
貪婪模式
在整個表達式匹配成功的前提下,盡可能多的匹配
非貪婪模式
在整個表達式匹配成功的前提下,盡可能少的匹配
舉個例子
要求:我想要匹配HTML頁面中三對段落標簽
中的兔子名稱。貪婪模式:
import rehtml = \ """<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>正則表達式</title> </head> <body><p>黑白道奇</p><p>垂耳兔</p><p>北極兔</p> </body> </html> """ pattern = re.compile(r'<p>.+</p>', re.S) list01 = pattern.findall(html) print(list01)結果:
['<p>黑白道奇</p>\n\t<p>垂耳兔</p>\n\t<p>北極兔</p>']貪婪模式中,列表中只匹配到了一個結果,這個結果顯然不符合我們的要求。
非貪婪模式:
import rehtml = \ """<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>正則表達式</title> </head> <body><p>黑白道奇</p><p>垂耳兔</p><p>北極兔</p> </body> </html> """ pattern = re.compile(r'<p>.+?</p>', re.S) list01 = pattern.findall(html) print(list01)結果:
['<p>黑白道奇</p>', '<p>垂耳兔</p>', '<p>北極兔</p>']非貪婪模式下,我們的列表中存放了3個結果,且每一對段落標簽中都有我們的兔子名稱,基本符合我們的要求。
為啥說基本符合要求,而不是完全滿足?因為我們只想要兔子名稱,不想要段落標簽
。所以,此時,我們就要使用正則表達式中的分組來解決這個問題了!正則表達式分組
分組的詳細知識點,請看正則表達式教程。
作用
在完整的模式中定義子模式,將每個圓括號中的子模式匹配出來的結果提取出來。
爬蟲應用
①在網頁中,想要提取什么內容,就在正則表達式中對應位置加()
②如果有2個及以上分組(),則結果中以元組形式顯示 [(),(),()]
舉個例子1
代碼:
import res = 'A B C D' p1 = re.compile('\w+\s+\w+') print(p1.findall(s))#1個子組 p2 = re.compile('(\w+)\s+\w+') #只輸出子組中匹配結果 print(p2.findall(s))#多個子組 p3 = re.compile('(\w+)\s+(\w+)') #按照元組形式,將各個子組中匹配結果輸出 print(p3.findall(s))結果:
['A B', 'C D'] ['A', 'C'] [('A', 'B'), ('C', 'D')]舉個例子2
我們有如下HTML頁面結構:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>正則表達式</title> </head> <body><div class="animal"><p class="name"><a title="Tiger"></a></p><p class="content">Two tigers two tigers run fast</p></div><div class="animal"><p class="name"><a title="Rabbit"></a></p><p class="content">Small white rabbit white and white</p></div></body> </html>要求:我們想要提取每個div標簽中,段落標簽p內,標簽a里的title參數值【“Tiger”、“Rabbit”】和字符串【Two tigers two tigers run fast、Small white rabbit white and white】
代碼:
import rehtml = \ """ <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>正則表達式</title> </head> <body><div class="animal"><p class="name"><a title="Tiger"></a></p><p class="content">Two tigers two tigers run fast</p></div><div class="animal"><p class="name"><a title="Rabbit"></a></p><p class="content">Small white rabbit white and white</p></div></body> </html> """pattern1 = re.compile(r'<div class="animal">.+?<p class="name">.+?<a title=(.+?)></a>', re.S) pattern2 = re.compile(r'<div class="animal">.+?<p class="content">(.+?)</p>', re.S)s01 = pattern1.findall(html) s02 = pattern2.findall(html)print(s01) print(s02)結果:
['"Tiger"', '"Rabbit"'] ['\n\t\t\tTwo tigers two tigers run fast\n\t ', '\n\t\t\tSmall white rabbit white and white\n\t ']當然,可以把兩個子組放在一個正則表達式中,那么輸出結果,就是一個包含兩個元祖的列表。在這里,我就不寫了,大家可以自己試一下。
現在我們從列表里拿出我們的值,并將值左右兩側的空格去掉:
for item01, item02 in zip(s01, s02):print('動物名稱:', item01.strip())print('動物描述:', item02.strip())結果:
動物名稱: "Tiger" 動物描述: Two tigers two tigers run fast 動物名稱: "Rabbit" 動物描述: Small white rabbit white and white總結
以上是生活随笔為你收集整理的利用python爬虫(part3)--正则表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用python爬虫(part2)--u
- 下一篇: CAD单击另存为没有对话框该怎么办(周站