這篇文章主要介紹“Python怎么開發(fā)個(gè)人專屬表情包網(wǎng)站”,在日常操作中,相信很多人在Python怎么開發(fā)個(gè)人專屬表情包網(wǎng)站問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Python怎么開發(fā)個(gè)人專屬表情包網(wǎng)站”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

環(huán)境:Windows+Python3.6
IDE:個(gè)人喜好
模塊
import requests import re import pymysq
完整代碼
import requests
import re
import pymysql
# 連接數(shù)據(jù)庫(kù)
db = pymysql.connect(host = '127.0.0.1',port = 3306,db = 'db',user = 'root',passwd = 'root',charset = 'utf8')
# 創(chuàng)建游標(biāo)
cursor = db.cursor()
# cursor.execute('select * from images')
# print(cursor.fetchall())
# 小駝峰
# 注釋 獲取圖片列表
def getImagesList(page):
# 獲取斗圖網(wǎng)源代碼
html = requests.get('http://www.doutula.com/photo/list/?page={}'.format(page)).text
# 正則表達(dá)式 通配符 .*? 匹配所有 分組匹配
reg = r'data-original="(.*?)".*?alt="(.*?)"'
# 增加匹配效率的 S 多行匹配
reg = re.compile(reg,re.S)
imagesList = re.findall(reg,html)
for i in imagesList:
image_url = i[0]
image_title = i[1]
# format 字符串格式化 %s
cursor.execute("insert into images(`name`,`imageUrl`) values('{}','{}') ".format(image_title,image_url))
print('正在保存 %s'%image_title)
db.commit()
# range 范圍 1<=X<1000
for i in range(1,1001):
print('第{}頁(yè)'.format(i))
getImagesList(i)效果圖

網(wǎng)站開發(fā)
使用的框架是Flask
from flask import Flask
from flask import render_template
from flask import request
import pymysql
# 404 頁(yè)面未找到
app = Flask(__name__)
# 裝飾器
@app.route('/') # route 路由
def index():
# return "hello world"
return render_template('index.html')
@app.route('/search')
def search():
# 接收用戶關(guān)鍵字
keyword = request.args.get('kw')
count = request.args.get('count')
cursor.execute("select * from images where name like '%{}%'".format(keyword))
data = cursor.fetchmany(int(count))
return render_template('index.html',images = data)
# 程序的入口
if __name__ == '__main__':
db = pymysql.connect(host='127.0.0.1', port=3306, db='db', user='root', passwd='root', charset='utf8',cursorclass = pymysql.cursors.DictCursor)
# 創(chuàng)建游標(biāo)
cursor = db.cursor()
# 調(diào)試模式
# port 端口號(hào) 默認(rèn)5000
app.run(debug=True,port=8000)運(yùn)行效果圖

到此,關(guān)于“Python怎么開發(fā)個(gè)人專屬表情包網(wǎng)站”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
網(wǎng)站題目:Python怎么開發(fā)個(gè)人專屬表情包網(wǎng)站-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://www.chinadenli.net/article46/dchdhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、電子商務(wù)、標(biāo)簽優(yōu)化、微信小程序、企業(yè)網(wǎng)站制作、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容