利用Python為女神制作一個專屬網(wǎng)站
先來看一下效果吧,只要有足夠的照片素材,捕獲女神的心就指日可待
怎么樣,看起來還可以吧
下面就一起來完成吧
數(shù)據(jù)準(zhǔn)備
首先是測試圖片的獲取,畢竟蘿卜哥當(dāng)前還沒有那么多女神的照片
這里我使用如下網(wǎng)站的高清圖片,嗯,各個都是大美女
抓取的代碼比較簡單
import?requests import?json def?get_pic(): ????headers?=?{"Accept":?"application/json,?text/javascript,?*/*;?q=0.01", ???????????????"User-Agent":?"Mozilla/5.0?(Macintosh;?Intel?Mac?OS?X?11_0_1)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/89.0.4389.114?Safari/537.36", ???????????????"Cookie":?"Hm_lvt_6e8dac14399b608f633394093523542e=1607173561;?Hm_lvt_ea4269d8a00e95fdb9ee61e3041a8f98=1621344383;?Hm_lpvt_ea4269d8a00e95fdb9ee61e3041a8f98=1621344423", ???????????????"Referer":?"http://lab.mkblog.cn/wallpaper/"} ????pic_url?=?"http://lab.mkblog.cn/wallpaper/api.php?cid=6&start=0&count=100" ????pic_res?=?requests.get(pic_url,?headers=headers) ????pic_res_json?=?pic_res.json() ????pic_info?=?pic_res_json.get("data") ????pic_url?=?[] ????num?=?0 ????try: ????????for?i?in?pic_info: ????????????if?num?%?5?==?0: ????????????????pic_url.append(i["url"]) ????????????if?num?%?5?==?1: ????????????????pic_url.append(i["img_1600_900"]) ????????????if?num?%?5?==?2: ????????????????pic_url.append(i["img_1366_768"]) ????????????if?num?%?5?==?3: ????????????????pic_url.append(i["img_1280_800"]) ????????????if?num?%?5?==?4: ????????????????pic_url.append(i["img_1024_768"]) ????????????num?+=?1 ????except: ????????pass ????return?pic_url def?save_pic_url(data): ????json.dump(data,?open("pic_url.json",?'w')) if?__name__?==?'__main__': ????pic_url?=?get_pic() ????save_pic_url(pic_url)
因為網(wǎng)站提供了不同分辨率的圖片,所以也就根據(jù)一定的規(guī)則來獲取不同分辨率的圖片了。
接下來是獲取渣男話術(shù),哈哈哈哈,又是一個有趣的網(wǎng)站,感興趣的朋友自行查看吧
由于這個接口是有調(diào)用頻率限制的,那么也抓取一些到本地吧
def?get_data(): ????headers?=?{"Accept":?"application/xml", ???????????????"User-Agent":?"Mozilla/5.0?(Macintosh;?Intel?Mac?OS?X?11_0_1)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/89.0.4389.114?Safari/537.36"} ????url?=?"https://nihaowua.90so.net/api/wus" ????res?=?requests.get(url,?headers=headers).json() ????return?res if?__name__?==?'__main__': ????data_list?=?[] ????for?i?in?range(10): ????????data?=?get_data() ????????data_list.append(data.get("title")) ????????time.sleep(10) ????json.dump(data_list,?open("data.json",?"w"))
網(wǎng)站搭建
首先我們還是通過簡單的 Flask 來進(jìn)行后臺的搭建
index 視圖
@app.route('/',?methods=['GET',?'POST']) def?index(): ????pic_list?=?json.load(open("pic_url.json")) ????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(pic_url) ????????tmp_data.append(pic_list.index(n)) ????????data.append(tmp_data) ????return?render_template('index.html',?data=data,?score=socre)
還是比較簡單的,拿到圖片地址文件中的數(shù)據(jù)后,根據(jù)規(guī)則展示一部分圖片
下面是 index.html 的部分核心代碼
圖片展示代碼
{%?for?p?in?data?%} ????????????<article?class="white-panel"> ????????????<img?class="thumb"?data-original="{{?p[0]?}}"> ????????????????<h1><a?href="{{?url_for('nvshen',?id=p[1])?}}" rel="external nofollow" ?title="去投票"?target="_blank">愛你??</a> ????????????????</h1> ????????</article> ????????{%?endfor?%}
懶加載圖片的 js 代碼
function?getData(page)?{ ????????????var?xhr?=?new?XMLHttpRequest(); ????????????xhr.responseType?=?"json"; ????????????xhr.open('POST',?'/api/getdata/'?+?page,?true); ????????????xhr.setRequestHeader("Content-Type",?"application/x-www-form-urlencoded"); ????????????xhr.onload?=?function?(ev)?{ ????????????????if(this.status?===?200)?{ ????????????????????if(this.response['end']?===?true)?{ ????????????????????????flag?=?false; ????????????????????} ????????????????????????var?mydata?=?this.response['msg']; ????????????????????????//console.log(mydata[1][2]); ????????????????????????for(var?i=0,?len=mydata.length;?i<len;?i++){ ????????????????????????????var?myurl?=?mydata[i][0]; ????????????????????????????var?htmlText?=?'<article?class="white-panel">'?+ ????????????????????????????????'<img?data-original='?+?myurl?+'?class="thumb">'?+ ????????????????????????????????'<h1>'?+ ????????????????????????????????'<a?href=URL?title="去投票"?target="_blank">'.replace("URL",?Flask.url_for("nvshen",?{id:?"1"}))?+ ?????????????????????????????????"愛你??"?+?'</a>'?+ ????????????????????????????????'</h1>'?+ ????????????????????????????????'<p>'?+ ????????????????????????????????'<div?id="starBg"?class="stars-bg">'?+ ????????????????????????????????'{%?if?score?==?1?%}'?+ ????????????????????????????????'<a?href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ?class="star-active"?style="width:?20%"></a>'?+ ????????????????????????????????'{%?elif?score?==?2?%}'?+ ????????????????????????????????'<a?href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ?class="star-active"?style="width:?40%"></a>'?+ ????????????????????????????????'{%?elif?score?==?3?%}'?+ ????????????????????????????????'<a?href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ?class="star-active"?style="width:?60%"></a>'?+ ????????????????????????????????'{%?elif?score?==?4?%}'?+ ????????????????????????????????'<a?href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ?class="star-active"?style="width:?80%"></a>'?+ ????????????????????????????????'{%?elif?score?==?5?%}'?+ ????????????????????????????????'<a?href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ?class="star-active"?style="width:?100%"></a>'?+ ????????????????????????????????'{%?else?%}'?+ ????????????????????????????????'<a?href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ?class="star-active"?style="width:?0%"></a>'?+ ????????????????????????????????'{%?endif?%}'?+ ????????????????????????????????'</div>'?+ ????????????????????????????????'</p>'?+ ????????????????????????????????'</article>'; ????????????????????????????var?script?=?'<script>'?+ ????????????????????????????????????'$(function(){'?+ ????????????????????????????????????'$("img.thumb").lazyload();'?+ ????????????????????????????????????'})'?+ ????????????????????????????????????'<\/script>'; ????????????????????????????$('#gallery-wrapper').append(htmlText); ????????????????????????????$('body').append(script); ????????????????????????} ????????????????}else?if(this.status?===?422)?{ ????????????????????console.log("get?data?error"); ????????????????} ????????????}; ????????????xhr.send(); ????????}
這里用到了 接口 getdata,我們來看看其實現(xiàn)
@app.route('/api/getdata/<int:page>',?methods=['POST']) def?get_data(page): ????pic_list?=?json.load(open("pic_url.json")) ????seg?=?0 ????seg_page?=?int(len(pic_list)/4) ????end?=?False ????if?page?==?2: ????????seg?=?seg_page ????????seg_page?=?seg*2 ????elif?page?==?3: ????????seg?=?seg_page*2 ????????seg_page?=?seg?+?seg_page ????elif?page?==?4: ????????seg?=?seg_page*3 ????????seg_page?=?int(len(pic_list))?+?1 ????????end?=?True ????elif?page?==?1: ????????pass ????else: ????????return?jsonify({"msg":?"error?page?id",?"code":?422}),?422 ????data?=?[] ????socre?=?1 ????for?n?in?pic_list[seg:seg_page]: ????????tmp_data?=?[] ????????pic_url?=?random.choice(pic_list) ????????tmp_data.append(pic_url) ????????data.append(tmp_data) ????return?jsonify({"msg":?data,?"code":?200,?"end":?end}),?200
這里有一個分頁的機(jī)制,用于懶加載圖片
接下來就是詳情頁,也就是展示“渣男語錄”的頁面
@app.route('/nvshen/<id>/',?methods=['GET',?'POST']) def?nvshen(id): ????pic_list?=?json.load(open("pic_url.json")) ????pic_url?=?pic_list[int(id)] ????data?=?json.load(open("data.json")) ????return?render_template('nvshen.html',?nvshenid=id,?main_url=pic_url,?data_list=data,?user_score=5)
分別拿到當(dāng)前圖片的地址和抓取好的渣男語錄,返回給前端
對于 nvshen.html 代碼也比較簡單,直接來個循環(huán)即可
????<section?id="gallery-wrapper"> ????????{%?for?d?in?data_list?%} ????????????<article?class="white-panel"> ????????????<!--<img?data-original="{{?d?}}"?class="thumb">--> ????????????????<h1><a?href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{?d?}}</a> ????????????????</h1> ????????</article> ????????{%?endfor?%} ????</section>
這樣,我們一個簡單的女神網(wǎng)站就完成了
服務(wù)部署
Python web 的部署,我一般使用 gunicorn
gunicorn 基本配置
#?coding=utf-8 import?sys import?os import?multiprocessing path_of_current_file?=?os.path.abspath(__file__) path_of_current_dir?=?os.path.split(path_of_current_file)[0] _file_name?=?os.path.basename(__file__) sys.path.insert(0,?path_of_current_dir) worker_class?=?'sync' workers?=?multiprocessing.cpu_count()?*?2?+?1 chdir?=?path_of_current_dir worker_connections?=?1000 timeout?=?30 max_requests?=?2000 graceful_timeout?=?30 loglevel?=?'info' reload?=?True debug?=?False access_log_format??=?'%(h)s?%(l)s?%(u)s?%(t)s?"%(r)s"?%(s)s?%(b)s?"%(f)s"?"%(a)s"?"%({X-Real-IP}i)s"' bind?=?"%s:%s"?%?("127.0.0.1",?5001) pidfile?=?'%s/logs/%s.pid'?%?(path_of_current_dir,?_file_name) errorlog?=?'%s/logs/%s_error.log'?%?(path_of_current_dir,?_file_name) accesslog?=?'%s/logs/%s_access.log'?%?(path_of_current_dir,?_file_name)
然后再用如下命令就可以啟動了
/root/miniconda3/bin/gunicorn?-D?-c?/home/nvshen/app/gunicorn?app:app
好了,這樣就可以把網(wǎng)站地址告訴女神了,坐等被夸!
到此這篇關(guān)于利用Python為女神制作一個專屬網(wǎng)站的文章就介紹到這了,更多相關(guān)Python制作網(wǎng)站內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)希爾伯特變換(Hilbert transform)的示例代碼
希爾伯特變換(Hilbert transform)是一個對函數(shù)產(chǎn)生定義域相同的函數(shù)的線性算子,而且希爾伯特變換在信號處理中很重要,所以本文和大家分享了Python實現(xiàn)希爾伯特變換的代碼,需要的可以參考一下2023-04-04Python獲取excel內(nèi)容及相關(guān)操作代碼實例
這篇文章主要介紹了Python獲取excel內(nèi)容及相關(guān)操作代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08python 3.74 運行import numpy as np 報錯lib\site-packages\numpy\_
這篇文章主要介紹了python 3.74 運行import numpy as np 報錯lib\site-packages\numpy\__init__.py,原來需要更新一下numpy即可2019-10-10python3.8 微信發(fā)送服務(wù)器監(jiān)控報警消息代碼實現(xiàn)
這篇文章主要介紹了python3.8 微信發(fā)送服務(wù)器監(jiān)控報警消息代碼實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11Pandas實現(xiàn)數(shù)據(jù)拼接的操作方法詳解
Python處理大規(guī)模數(shù)據(jù)集的時候經(jīng)常需要使用到合并、鏈接的方式進(jìn)行數(shù)據(jù)集的整合,本文為大家主要介紹了.merge()、?.join()?和?.concat()?三種方法,感興趣的可以了解一下2022-04-04使用Python下載歌詞并嵌入歌曲文件中的實現(xiàn)代碼
這篇文章主要介紹了使用Python下載歌詞并嵌入歌曲文件中的實現(xiàn)代碼,需要借助eyed3模塊,需要的朋友可以參考下2015-11-11python利用不到一百行代碼實現(xiàn)一個小siri
這篇文章主要介紹了關(guān)于python利用不到一百行代碼實現(xiàn)了一個小siri的相關(guān)資料,文中介紹的很詳細(xì),對大家具有一定的參考借鑒價值,需要的朋友們下面來一起看看吧。2017-03-03