Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集
知識點
- requests
- parsel
- re
- os
環(huán)境
- python3.8
- pycharm2021
目標網(wǎng)址:
https://mm.enterdesk.com/bizhi/63899-347866.html
【付費VIP完整版】只要看了就能學會的教程,80集Python基礎(chǔ)入門視頻教學
點這里即可免費在線觀看
注意: 在我們查看網(wǎng)頁源代碼的時候 (1. 控制臺為準 2. 以右鍵查看網(wǎng)頁源代碼 3. 元素面板)
- 發(fā)送網(wǎng)絡(luò)請求
- 獲取網(wǎng)頁源代碼
- 提取想要的圖片鏈接 css樣式提取 xpath re正則表達式 bs4
- 替換所有的圖片鏈接 換成大圖
- 保存圖片
爬蟲代碼
導(dǎo)入模塊
import requests # 第三方庫 pip install requests import parsel # 第三方庫 pip install parsel import os # 新建文件夾
發(fā)送網(wǎng)絡(luò)請求
response = requests.get('https://mm.enterdesk.com/bizhi/64011-348522.html')
獲取網(wǎng)頁源代碼
data_html = response_1.text
提取每個相冊的詳情頁鏈接地址
selector_1 = parsel.Selector(data_html) photo_url_list = selector_1.css('.egeli_pic_dl dd a::attr(href)').getall() title_list = selector_1.css('.egeli_pic_dl dd a img::attr(title)').getall() for photo_url, title in zip(photo_url_list, title_list): print(f'*****************正在爬取{title}*****************') response = requests.get(photo_url) # <Response [200]>: 請求成功的標識 selector = parsel.Selector(response.text) # 提取想要的圖片鏈接[第一個鏈接, 第二個鏈接,....] img_src_list = selector.css('.swiper-wrapper a img::attr(src)').getall() # 新建一個文件夾 if not os.path.exists('img/' + title): os.mkdir('img/' + title)
替換所有的圖片鏈接 換成大圖
for img_src in img_src_list: # 字符串的替換 img_url = img_src.replace('_360_360', '_source')
保存圖片 圖片名字
# 圖片 音頻 視頻 二進制數(shù)據(jù)content img_data = requests.get(img_url).content # 圖片名稱 字符串分割 # 分割完之后 會給我們返回一個列表 img_title = img_url.split('/')[-1] with open(f'img/{title}/{img_title}', mode='wb') as f: f.write(img_data) print(img_title, '保存成功!!!')
翻頁
page_html = requests.get('https://mm.enterdesk.com/').text counts = parsel.Selector(page_html).css('.wrap.no_a::attr(href)').get().split('/')[-1].split('.')[0] for page in range(1, int(counts) + 1): print(f'------------------------------------正在爬取第{page}頁------------------------------------') 發(fā)送網(wǎng)絡(luò)請求 response_1 = requests.get(f'https://mm.enterdesk.com/{page}.html')
爬取結(jié)果
到此這篇關(guān)于Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集的文章就介紹到這了,更多相關(guān)Python 圖片采集內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。