目錄
一、目標1:正則匹配圖片的URL
二、目標2:創(chuàng)建文件夾
三、目標3:保存圖片到test文件夾中
?四、網(wǎng)絡(luò)安全小圈子
一、目標1:正則匹配圖片的URL
URL位置
我們可以找到img都在這個標簽里面
?
?
請求方法GET
?
提取URL位置
需要掌握的關(guān)鍵字
.*?
//表示匹配任意字符(除換行符)
(.*?)
//表示匹配任意字符(除換行符)0次或多次,盡可能少地匹配,并將這部分內(nèi)容作為一個分組
目標標簽如下:
<div class="thumb">
<a target="_blank" style="background:url(https://pic.chinaz.com/picmap/thumb/202306271716277907_5.jpg);background-size:cover;background-position:center" rel="noopener noreferrer" >
</a>
</div>
需要提取的內(nèi)容如下:
(多加了一個\為轉(zhuǎn)義字符)
img_url = <div class="thumb">.*?url\((.*?)).*?</div>
爬取到所有的格式相符的圖片內(nèi)容
res:是一個正則表達式,用于匹配的模式。
img_url:是要進行匹配的字符串。
re.S:是re模塊中的一個標志參數(shù),表示將字符串視為單行,即將換行符也視為普通字符
img_url_list = re.findall(res,img_url,re.S)
運行結(jié)果:
將目標URL都爬取到了
?
完整代碼:
import re
import requests
def get_img():
url = 'https://www.chinaz.com/'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0",
}
res = requests.get(url,headers=headers,timeout=10).text
img_url = '<div class="thumb">.*?url\((.*?));.*?</div>'
img_url_list = re.findall(img_url,res,re.S)
print(img_url_list)
if __name__ == '__main__':
get_img()
二、目標2:創(chuàng)建文件夾
判斷文件夾是否存在來決定是否創(chuàng)建文件夾,并使用os.makedirs()函數(shù)遞歸創(chuàng)建文件夾。如果文件夾已存在,則不會進行任何操作
('./test'也可以換為參數(shù)進行傳值)
(os.mkdir()函數(shù)是不會遞歸創(chuàng)建文件夾)
import os
if not os.path.exists('./test'):
os.makedirs('./test')
三、目標3:保存圖片到test文件夾中
處理數(shù)據(jù)
首先依次遍歷圖片的URL,然后除去非必要的字符
使用replace()函數(shù)將非必要字符替換為空
for u in img_url_list:
img_u = u.replace("(", "").replace(")", "")
print(img_u)
運行后,文件夾已創(chuàng)建,URL也遍歷成功
?
獲取圖片數(shù)據(jù)
有圖片地址后get就可以請求到
.content返回二進制格式數(shù)據(jù)
image = requests.get(url=img_u,headers=headers,timeout=10).content
給文件命名
取url最后的那個為他的名字
也就是https://pic.chinaz.com/picmap/thumb/202306271716277907_5.jpg
取????????202306271716277907_5.jpg
以此類推
img_name = img_u.split('/')[-1]
將圖片保存到本地
圖片路徑
下載圖片到本地路徑
img_path = './test/' + img_name
with open(img_path,'wb') as f:
f.write(img_data)
print(img_name + '下載成功!')
運行結(jié)果:
發(fā)現(xiàn)還有一個雜數(shù)據(jù)進入了導(dǎo)致報錯
?
然后我果斷加了一個if判斷語句
if re.match(r'https?://', u):
?最后下載成功了
?
完整代碼:
import re
import requests
import os
def get_img():
url = 'https://www.chinaz.com/'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0",
}
res = requests.get(url, headers=headers, timeout=10).text
img_url = r'<div class="thumb">.*?url\((.*?)\);.*?</div>'
img_url_list = re.findall(img_url, res, re.S)
for u in img_url_list:
if re.match(r'https?://', u):
img_u = u.replace("(", "").replace(")", "")
img_data = requests.get(url=img_u, headers=headers, timeout=10).content
img_name = img_u.split('/')[-1]
img_path = './test/' + img_name
with open(img_path, 'wb') as f:
f.write(img_data)
print(img_name + '下載成功!')
else:
continue
if __name__ == '__main__':
if not os.path.exists('./test'):
os.makedirs('./test')
get_img()
?四、網(wǎng)絡(luò)安全小圈子
README.md · 書半生/網(wǎng)絡(luò)安全知識體系-實戰(zhàn)中心 - 碼云 - 開源中國 (gitee.com)https://gitee.com/shubansheng/Treasure_knowledge/blob/master/README.md文章來源:http://www.zghlxwxcb.cn/news/detail-546491.html
GitHub - BLACKxZONE/Treasure_knowledgehttps://github.com/BLACKxZONE/Treasure_knowledge文章來源地址http://www.zghlxwxcb.cn/news/detail-546491.html
到了這里,關(guān)于【網(wǎng)絡(luò)安全帶你練爬蟲-100練】第10練:re的正則匹配、文件夾創(chuàng)建、圖片下載的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!