新聞中心
廢話不多說,咱們直接上最終的效果圖

成都創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網站、網站重做改版、西山網站定制設計、自適應品牌網站建設、H5高端網站建設、商城網站建設、集團公司官網建設、外貿營銷網站建設、高端網站制作、響應式網頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為西山等各大城市提供網站開發(fā)制作服務。
圖片
圖片
我們獲取圖片的目標地址是 360 壁紙庫,網上有大神已經做過一波分析了,我們直接拿來使用
https://mkblog.cn/581/
美圖獲取
我們首先獲取壁紙分類信息
先使用 postman 調用,查看響應數據情況
圖片
使用代碼保存分類信息
import requests
import json
import time
category = requests.get("http://cdn.apc.#/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome")
category_list = category.json()['data']
# 保存category到json文件
category_list
with open("categoty.json",'w', encoding='utf-8') as file_obj:
json.dump(category_list, file_obj, ensure_ascii=False, indent=4)接下來再看下具體的獲取圖片的接口情況
圖片
同樣可以根據響應信息,來編寫解析代碼
def get_pic(categoty, count):
for i in range(1, 100):
pic_list = []
pic_url = "http://wallpaper.apc.#/index.php?c=WallPaper&a=getAppsByCategory&cid=%s&start=%s&count=%s&from=360chrome" % (categoty, str(i), count)
pic = requests.get(pic_url)
pic_data = pic.json()["data"]
if pic_data:
tmp = deal_pic_data(pic_data)
else:
break
time.sleep(5)其中在函數 deal_pic_data 當中,我們調用了兩個子函數,分別用來下載圖片和 tag 信息
def download_img(img_url, name):
print (img_url)
r = requests.get(img_url, stream=True)
print(r.status_code) # 返回狀態(tài)碼
if r.status_code == 200:
open("pic\\" + name + '_img.png', 'wb').write(r.content) # 將內容寫入圖片
print("done")
del r
def save_tag(tag, name):
print(tag)
with open("tag\\" + name + ".txt", "w") as f:
f.write(tag)下圖即為爬取過程
圖片
最終我們在本地就成功保存了上千張小姐姐照片
圖片
你以為這樣就結束了嗎,當然沒有
制作網站
畢竟這么多的小姐姐,都在文件夾里是多么的不方便查看呀,我們做成 web 瀏覽起來是真的香!
我們先編寫 index 頁面的視圖函數
@app.route('/', methods=['GET', 'POST'])
def index():
pic_path = basedir + "\static\img\pic"
pic_list = os.listdir(pic_path)
seg = int(len(pic_list)/4)
data = []
socre = 5
for n in pic_list[:seg]:
tmp_data = []
pic_url = random.choice(pic_list)
tmp_data.append(r"\static\img\pic\\" + pic_url)
tmp_data.append(pic_list.index(n))
data.append(tmp_data)
return render_template('index.html', data=data, score=socre)我們從本地文件夾中拿到小姐姐圖片,然后組裝成需要的數據格式,傳遞給前端
對于 index.html 代碼
{% for p in data %}
{% endfor %}
在拿到后端傳遞的數據后,依次展示在 section 標簽中
接下來是詳情頁面
@app.route('/nvshen//', methods=['GET', 'POST'])
def nvshen(id):
pic_path = basedir + "\static\img\pic"
pic_list = os.listdir(pic_path)
pic_url = r"\static\img\pic\\" + pic_list[int(id)]
data = []
score_pic_path = r"static\img\pic\\" + pic_list[int(id)]
gender, age, female_score, male_score, emotion_data = fire_score(score_pic_path)
data.append('性別: %s' % gender)
data.append('年齡: %s' % age)
data.append('顏值評分: %s' % female_score)
data.append('情緒: %s' % emotion_data)
return render_template('nvshen.html', nvshenid=id, main_url=pic_url, data_list=data, user_score=5) 我這里調用了曠視 Face++ 的人臉識別接口,自動返回不同小姐姐的顏值信息
再來看看前端的 HTML 代碼
{% for d in data_list %}
{{ d }}
{% endfor %}
分別展示設置桌面按鈕和顏值信息卡片
最后我們再來看看如何設置桌面壁紙
可以看到在上面的代碼中,調用了一個 setWallpaper 函數
我們這里調用了后端的 setwallpaper 接口
@app.route("/setwallpaper/")
def setWallpaperView(pic):
try:
pic_path = basedir + "\static\img\pic\\" + pic
result = setWallpaper(pic_path)
return jsonify({"msg": "OK"}), 200
except Exception as e:
return jsonify({"msg": "ERROR"}), 422
import win32api
import win32gui
import win32con
def setWallpaper(imagepath):
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") # 2拉伸,0居中,6適應,10填充,0平鋪
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0") # 1表示平鋪,拉伸居中等都是0
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)
return "Set OK" 通過后端代碼,來進行桌面壁紙的設置,設置壁紙采用的是直接通過 win32gui 改寫注冊表信息
整體代碼下來,我們主要用到了 Python 爬蟲簡單技術,F(xiàn)lask 的簡單應用以及部分 HTML&JavaScript 技術,技術棧還是比較簡單的,喜歡的小伙伴一起來實現(xiàn)下吧
分享標題:Python輕松爬取上千張小姐姐圖片
URL網址:http://www.dlmjj.cn/article/dppipgd.html


咨詢
建站咨詢
