1. 程式人生 > 實用技巧 >現表情包大幅流行,今天教你們開發個人表情包網站

現表情包大幅流行,今天教你們開發個人表情包網站

本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯絡我們以作處理

以下文章來源於騰訊雲 作者:python學習教程

( 想要學習Python?Python學習交流群:1039649593,滿足你的需求,資料都已經上傳群檔案流,可以自行下載!還有海量最新2020python學習資料。 )

“表情包”是一種利用圖片來表示感情的一種方式。表情包是在社交軟體活躍之後,形成的一種流行文化,表情包流行於網際網路上面,基本人人都會發表情。

曾經你是否也有過找不到表情包去應對別人的時候。

今天小編分享如何用Python開發個人專屬的表情包網站,想用什麼表情包搜一下就有了!

本篇分為兩部分

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)

執行效果圖