爬蟲 之 re庫

2021-08-31 23:46:16 字數 788 閱讀 3163

a表示正則的規則,b表示字串

從開頭開始匹配,若開頭就匹配失敗,則返回為none

result = re.match('~~a~~','~~b~~')

result.group()

若a:的規則中有用 小括號() 圈起來東西,可以按順序由 result.group(1)    result.group(2)  .......等匹配得到

掃瞄整個字串,返回第乙個成功匹配正規表示式的內容

result = re.search('~~a~~','~~b~~')

result.group()

掃瞄整個字串,返回所有匹配正規表示式的內容(返回值是元組組成的列表)

results = re.findall(『~~a~~』,'~~b~~')

for result in results:

print(result)

替換正規表示式匹配中的內容

result = re.sub('~~a~~','','~~b~~')

print(result)

把匹配的內容刪去

復用正規表示式的規則

pattern = re.compile('~~a~~')

result1 = re.search(pattern, '~~b~~')

result2 = re.search(pattern, '~~c~~') # c 為另一組文字

ok

python爬蟲 re庫(正則)

1.re.match re.match嘗試從字元創的起始位置匹配乙個模式,如果不是起始位置匹配成功的話,就會返回none。re.match pattern,string,flags 0 2.最常規的匹配 import re content hello 123 4567 world this is a...

python爬蟲之re模組

傳送門python爬蟲之正規表示式 match 從開始的位置進行匹配。如果開始的位置沒有匹配到。就直接失敗了。text hello ret re.match h text print ret.group 在字串中找到第乙個滿足條件的。text hello ret re.search e text p...

爬蟲之re塊解析

這個去匹配比較麻煩,以後也比較少用,簡單看乙個案例就行 爬取資料流程 1.指定url 2.發起請求 3.獲取頁面資料 4.資料解析 5.持久化儲存 import requests import reimport osheaders url 獲取一整張頁面資料原始碼 page text request...