記錄小白學習python爬蟲的過程(二)

2021-10-09 10:40:32 字數 4749 閱讀 7447

正規表示式

^h表示以h開頭,.表示任意字元,*表示任意多次

import re

line =

'hello 123'

#^h表示以h開頭,.表示任意字元,*表示任意多次**

re_str =

'^h.*'

if re.match(re_str, line)

:print

('匹配成功'

)# 輸出:匹配成功

$表示結尾字元
import re

line =

'hello 123'

re_str =

'.*3$'

# 前面可為任意多個任意字元,但結尾必須是3

if re.match(re_str, line)

:print

('匹配成功'

)# 輸出:匹配成功

?表示非貪婪模式
import re

line =

'heeeello123'

re_str =

'.*?(h.*?l).*'

# 只要()中的子串

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)# 輸出:heeeel

# 如果去掉?,則輸出:heeeell

+表示至少出現一次
import re

line =

'heeeello123'

re_str =

'.*(h.+?l).*'

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)#輸出:heeeel

表示前面字元出現2次
import re

line =

'heeeello123'

re_str =

'.*?(e.?l).*'

# 匹配的是e+任意2個字元+l

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)# 輸出:eeel

| 表示或
import re

line =

'hello123'

re_str =

'((hello|heeello)123)'

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)# 輸出:python123

表示對單個字元給出取值範圍
import re

line =

'hello123'

re_str =

"([jhk]ello123)"

# [jhk]表示jhk中的任乙個都可以

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)# 輸出:hello123

[^]表示非字符集
import re

line =

'hello123'

re_str =

"([^j]ello123)"

# [^j]表示不是j的都行

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)# 輸出:hello123

\s表示空格 \s表示非空格
import re

line =

'hello123 好'

#字串有空格

re_str =

"(hello123\s好)"

# 匹配上空格

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)#輸出:hello123 好

[\u4e00-\u9fa5]表示漢字
import re

line =

'hello 北京大學'

re_str =

".*?([\u4e00-\u9fa5]+大學)"

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)# 輸出:北京大學

小例子 提取出生日期
import re

line =

'***出生於2023年6月1日'

line =

'***出生於2000/6/1'

line =

'***出生於2000-6-1'

line =

'***出生於2000-06-01'

line =

'***出生於2000-06'

re_str =

".*出生於(\d[年/-]\d([月/-]|\d|$))"

match_obj = re.match(re_str, line)

if match_obj:

print

(match_obj.group(1)

)

re庫主要功能函式:

函式說明

re.search()

在乙個字串中搜尋匹配正規表示式的第乙個位置,返回match物件

re.match()

從乙個字串的開始位置起匹配正規表示式,返回match物件

re.findall()

搜尋字串,以列表型別返回全部能匹配的字串

re.split()

將乙個字串按照正規表示式匹配結果進行分割,返回列表型別

re.finditer()

搜尋字串,返回乙個匹配結果的迭代型別,每個迭代元素是match物件

re.sub()

在乙個字串中替換所有匹配正規表示式的子串,返回替換後的字串

正規表示式的表示型別:

raw string型別(原生字串型別):

re庫採用raw string型別表示正規表示式,表示為:r』text』

例如:r』[1-9]\d』

raw string是指不包含轉義符的字串

string型別,更繁瑣。

例如:』[1-9]\d』;』\d-\d|\d-\d』

當正規表示式包含轉義符時,建議使用raw string型別來表示正規表示式。
import re

ss =

'i love you, do you?'

res = re.match(r'((\w)+(\w))+'

,ss)

print

(res.group(

))

i love you,
import re

ss =

'i love you, do you?'

res = re.search(r'(\w+)(,)'

,ss)

#print(res)

print

(res.group(0)

)print

(res.group(1)

)print

(res.group(2)

)

you,

you,

其他的就暫且不舉例了。

1.尋找ping資訊中的時間結果

import re

ping_ss =

'reply from 220.181.57.216:bytes=32 time=3ms ttl=47'

res = re.search(r'(time=)(\d+\w+)+(.)+ttl'

,ping_ss)

print

(res.group(2)

)

3ms
2.用來解析網頁

import re,requests

r = requests.get(

'').content.decode(

'utf-8'

)print

(r)pt = re.

compile

('(\)([\s\s]+)(\<\/title\>)'

)print

(pt.search(r)

.group(2)

)

另外,可以用字串前的「r」來提高效率

pt = re.

compile

(r'()([\s\s]+)()'

)

python小白學習記錄 爬蟲requests篇

一 引用庫 import requests 二 請求訪問url,網頁相應 res requests.get 網頁位址 三 表明返回內容 目前返回的response物件有四種屬性 status code 檢查請求是否成功 content 將資料轉換為二進位制資料 text 將資料轉換為字串型資料 en...

python爬蟲入門學習記錄

在使用爬蟲前確保requests和beautifulsoup4模組都已經安裝好了 pip install requests pip install beautifulsoup4 beautifulsoup4使用手冊 簡單的示列 import requests 匯入requests包 url strh...

學習筆記 關於小白學習Python爬蟲的一些筆記

requests與beautifulsoup爬取一些 的經驗 這是第一次寫的爬取 的程式,寫得不夠簡潔有些地方都寫的不是很規範,希望在以後能夠不斷勉勵寫出更好的 也做作為自己以後學習的乙個參考 import requests from bs4 import beautifulsoup import ...