用python爬點高質量的壁紙換換,每天保持心情愉悅!


每天我的壁紙都是Windows自帶的天藍色,看的真的沒意思,有意思嗎,沒意思~
所以啊,當然的整一手高質量壁紙,沒有別的意思,只為了心情愉悅~
 
好了,不多嗶嗶,開啟今天的高質量旅途~
 
 
一、准備工作
 
這些統統安排上
 
軟件
  • python 3.6
  • pycharm
模塊
  • requests
  • parsel
二、爬蟲流程
 
關於數據來源查找:
 
確定目標需求: 爬取高清壁紙圖片 (彼岸)
通過開發者工具(F12或者鼠標右鍵點擊檢查) 查找圖片的url地址來源;
請求 壁紙的詳情頁 獲取它網頁源代碼 就可以獲取圖片url地址了 (一張);
請求 列表頁就可以獲取 每個壁紙的詳情頁url 以及 標題;
 
代碼實現
 
1、發送請求
壁紙的列表頁url: http://www.netbian.com/1920x1080/index.htm
2、獲取數據
網頁源代碼/ response.text 網頁文本數據
3、解析數據
css xpath bs4 re 壁紙詳情頁url:/desk/23397.htm 2.壁紙標題
4、保存數據
保存圖片是二進制數據
 
觀眾姥爺:就這就這?代碼呢?代碼都不放你幾個意思?
 
 
別慌,來了來了
 
三、代碼展示
 
我就不一 一拆解了,注釋加上第三步,相信聰明的你可以理解,實在不行最后我放視頻講解吧。
 
import requests # 請求模塊 第三方模塊 pip install requests
import parsel # 數據解析模塊 第三方模塊 pip install parsel
import time # 時間模塊 內置模塊

time_1 = time.time()
# 要什么用模塊 首先要知道模塊有什么用
for page in range(2, 12):
    print(f'====================正在爬取第{page}頁的數據內容====================')
    url = f'http://www.netbian.com/1920x1080/index_{page}.htm'
    # 請求頭: 把python代碼偽裝成瀏覽器對服務器發送請求
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    # 出現亂碼怎么辦? 需要轉碼
    # html_data = response.content.decode('gbk')
    response.encoding = response.apparent_encoding # 自動轉碼
    # 獲取源代碼/獲取網頁文本數據 response.text
    # print(response.text)
    # 解析數據
    selector = parsel.Selector(response.text)
    # CSS選擇器 就是根據網頁標簽內容提取數據
    # 第一次提取 提取所有的li標簽內容
    lis = selector.css('.list li')
    for li in lis:
        # http://www.netbian.com/desk/23397.htm
        title = li.css('b::text').get()
        if title:
            href = 'http://www.netbian.com' + li.css('a::attr(href)').get()
            response_1 = requests.get(url=href, headers=headers)
            selector_1 = parsel.Selector(response_1.text)
            img_url = selector_1.css('.pic img::attr(src)').get()

            img_content = requests.get(url=img_url, headers=headers).content
            with open('img\\' + title + '.jpg', mode='wb') as f:
                f.write(img_content)
                print('正在保存: ', title)



time_2 = time.time()
use_time = int(time_2) - int(time_1)
print(f'總計耗時{use_time}秒')

我還給大家准備了這些資料,直接在這里免費領。

# 一群:872937351 (群滿了的話加二群)
# 二群:924040232
# python學習路線匯總
# 精品Python學習書籍100本
# Python入門視頻合集
# Python實戰案例
# Python面試題
# Python相關軟件工具/pycharm永久激活

 

大家可以自己運行試試,記得三連哇~

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM