前言
嗨嘍~大家好呀,這里是魔王吶 !
壁紙,有多種的類別和各種不同的風格,如:
風景、美女、唯美、動漫、花卉、節日等適合您的高清桌面壁紙
今天我們就來采集一下叭~
環境使用:
-
Python 3.8 解釋器
-
Pycharm 編輯器
需安裝python第三方模塊 : requests
-
win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車
-
在pycharm中點擊Terminal(終端) 輸入安裝命令
基本思路流程:
1. 發送請求
模擬瀏覽器 對于url地址發送請求, 獲取服務器回傳回應資料
偽裝 headers 請求頭
2. 獲取資料
3. 決議資料
提取我們想要的內容
4. 保存資料
代碼
import requests # 用來發送請求模塊<工具>
import re # 提取資料工具
response = requests.get(url=url, headers=headers) response.encoding = 'gbk' # 獲取網頁內容,回傳出現亂碼 print(response.text) # 獲取網頁源代碼 # 獲取壁紙名字以及壁紙詳情頁url地址 從什么地方找什么樣資料內容, 從response.text 里面找<li><a href=https://www.cnblogs.com/Qqun261823976/archive/2022/09/12/"(.*?)"title="(.*?)" target="_blank"> # (.*?) 就是我們想要資料 html_info = re.findall('<li><a href="https://www.cnblogs.com/Qqun261823976/archive/2022/09/12/(.*?)" title="(.*?)" target="_blank">', response.text) print(html_info)
response_1 = requests.get(url=link_url, headers=headers) response_1.encoding = 'gbk' # print(response_1.text) img_url = re.findall('<img src="https://www.cnblogs.com/Qqun261823976/archive/2022/09/12/(.*?)" alt=".*?"', response_1.text)[0] img_content = requests.get(url=img_url).content with open('img\\' + title + '.jpg', mode='wb') as f: f.write(img_content) print(img_url, title)
效果



尾語
要成功,先發瘋,下定決心往前沖!
學習是需要長期堅持的,一步一個腳印地走向未來!
未來的你一定會感謝今天學習的你,
—— 心靈雞湯
本文章到這里就結束啦~感興趣的小伙伴可以復制代碼去試試哦 ??
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/506528.html
標籤:其他
