前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
“表情包”是一種利用圖片來表示感情的一種方式,表情包是在社交軟體活躍之后,形成的一種流行文化,表情包流行于互聯網上面,基本人人都會發表情,
曾經你是否也有過找不到表情包去應對別人的時候,
今天小編分享如何用Python開發個人專屬的表情包網站,想用什么表情包搜一下就有了!
PS:如有需要Python學習資料的小伙伴可以加下方的群去找免費管理員領取
可以免費領取原始碼、專案實戰視頻、PDF檔案等
本篇分為兩部分
1、爬取表情包存入資料庫
2、搭建個人個人專屬表情網站
爬取表情包存入資料庫
環境:Windows+Python3.6
IDE:個人喜好
模塊
import requests import re import pymysq
完整代碼
import requests import re import pymysql # 連接資料庫 db = pymysql.connect(host = '127.0.0.1',port = 3306,db = 'db',user = 'root',passwd = 'root',charset = 'utf8') # 創建游標 cursor = db.cursor() # cursor.execute('select * from images') # print(cursor.fetchall()) # 小駝峰 # 注釋 獲取圖片串列 def getImagesList(page): # 獲取斗圖網源代碼 html = requests.get('http://www.doutula.com/photo/list/?page={}'.format(page)).text # 正則運算式 通配符 .*? 匹配所有 分組匹配 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('第{}頁'.format(i)) getImagesList(i)
效果圖
網站開發
使用的框架是Flask
from flask import Flask from flask import render_template from flask import request import pymysql # 404 頁面未找到 app = Flask(__name__) # 裝飾器 @app.route('/') # route 路由 def index(): # return "hello world" return render_template('index.html') @app.route('/search') def search(): # 接收用戶關鍵字 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) # 創建游標 cursor = db.cursor() # 除錯模式 # port 埠號 默認5000 app.run(debug=True,port=8000)
運行效果圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/61864.html
標籤:Python
