Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集
更新時間:2021年10月15日 10:11:15 作者:松鼠愛吃餅干
讀萬卷書不如行萬里路,學的扎不扎實要通過實戰(zhàn)才能看出來,今天小編給大家?guī)硪粋€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基礎入門視頻教學
注意: 在我們查看網(wǎng)頁源代碼的時候 (1. 控制臺為準 2. 以右鍵查看網(wǎng)頁源代碼 3. 元素面板)
- 發(fā)送網(wǎng)絡請求
- 獲取網(wǎng)頁源代碼
- 提取想要的圖片鏈接 css樣式提取 xpath re正則表達式 bs4
- 替換所有的圖片鏈接 換成大圖
- 保存圖片
爬蟲代碼
導入模塊
import requests # 第三方庫 pip install requests import parsel # 第三方庫 pip install parsel import os # 新建文件夾
發(fā)送網(wǎng)絡請求
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)絡請求
response_1 = requests.get(f'https://mm.enterdesk.com/{page}.html')爬取結果



到此這篇關于Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集的文章就介紹到這了,更多相關Python 圖片采集內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python處理自動化任務之同時批量修改word里面的內(nèi)容的方法
在本篇文章里小編給各位整理的是一篇關于利用python處理自動化任務之同時批量修改word里面的內(nèi)容的文章,需要的可以參考學習下。2019-08-08

