兄弟們,上一個系列大家多少有點不太喜歡,那今天上點不一樣的,

來吧,直接整活~
先準備一下
首先咱們需要安裝一下這兩個第三方模塊
requests >>> # pip install requests
parsel >>> # pip install parsel
不會安裝的小伙伴,鍵盤按住win+r 在彈出來的運行框 輸入cmd 按確定,然后彈出黑色的框框,即命令提示符視窗,輸入上面的注釋 ,然后按回車即可安裝成功,

實作步驟
第一步:找資料對應的鏈接地址
第二步:python代碼發送指定地址的請求
第三步:資料決議(篩選 過濾咱們需要的資料)
決議了每一個相冊地址之后
- a 創建每一個相冊的檔案夾
- b 請求每一個相冊詳情頁
(1) 發送請求
(2) 決議資料 提取圖片鏈接
(3) 依次請求圖片
第四步:保存資料
代碼部分
import requests import parsel # 決議資料 import os # Python學習交流 1群 815624229 快滿了加2裙 # Python學習交流 2群 279199867 for page in range(1, 12): response = requests.get(f'https://www.jdlingyu.com/tag/%e8%85%bf%e6%8e%a7/page/{page}') # <Response [200]>: 請求成功 # 靜態頁面的網頁源代碼 # 把靜態頁面的 字串 變成可選的物件 selector = parsel.Selector(response.text) # 3. 資料決議(篩選 過濾咱們需要的資料) # re: 可以直接提取網頁字串資料 # css / xpath: 通過HTML標簽進行提取 lis = selector.xpath("//ul[@class='b2_gap ']/li") for li in lis: # text(): 提取標簽的文本內容 title = li.xpath('.//h2/a/text()').get() print(f'-------------正在爬取{title}-------------') # a 如果當前檔案夾不存在 那么就新建一個 if not os.path.exists('./img/'+title): os.mkdir('./img/'+title) # 每一個相冊所在的 網頁地址 # 獲取a標簽屬性內容 /@src href = https://www.cnblogs.com/hahaa/p/li.xpath('.//h2/a/@href').get() # b 請求每一個相冊詳情頁 data_html = requests.get(href).text # 把字串物件轉變為 可選的selector物件 selector_2 = parsel.Selector(data_html) img_p = selector_2.xpath('//div[@]/p') for img in img_p: # (2) 決議資料 提取圖片鏈接 img_list = img_p.xpath('.//img/@src').getall() for img_1 in img_list: # content: 獲取二進制資料 圖片 視頻 音頻 # 文本: text try: img_data = requests.get(img_1).content # 圖片檔案名 except: continue img_name = img_1.split('/')[-1] with open(f'img\\{title}\\{img_name}', mode='wb') as f: f.write(img_data) print('下載完成:', img_name)
效果展示


這個分類總共是12頁,我就不全部展示了,大家可以自己去試試!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/466962.html
標籤:Python
