“表情包”是一种利用图片来表示感情的一种方式。表情包是在社交软件活跃之后,形成的一种流行文化,表情包流行于互联网上面,基本人人都会发表情。
曾经你是否也有过找不到表情包去应对别人的时候。
今天小编分享如何用Python开发个人专属的表情包网站,想用什么表情包搜一下就有了!

本篇分为两部分
1、爬取表情包存入数据库
2、搭建个人个人专属表情网站
爬取包情包存入数据库
环境:Windows+Python3.6
IDE:个人喜好
模块
1 import requests 2 import re 3 import pymysq
完整代码
6 import requests
7 import re
8 import pymysql
9 # 连接数据库
10 db = pymysql.connect(host = '127.0.0.1',port = 3306,db = 'db',user = 'root',passwd = 'root',charset = 'utf8')
11 # 创建游标
12 cursor = db.cursor()
13 # cursor.execute('select * from images')
14 # print(cursor.fetchall())
15 # 小驼峰
16 # 注释 获取图片列表
17 def getImagesList(page):
18 # 获取斗图网源代码
19 html = requests.get('http://www.doutula.com/photo/list/?page={}'.format(page)).text
20 # 正则表达式 通配符 .*? 匹配所有 分组匹配
21 reg = r'data-original="(.*?)".*?alt="(.*?)"'
22 # 增加匹配效率的 S 多行匹配
23 reg = re.compile(reg,re.S)
24 imagesList = re.findall(reg,html)
25 for i in imagesList:
26 image_url = i[0]
27 image_title = i[1]
28 # format 字符串格式化 %s
29 cursor.execute("insert into images(`name`,`imageUrl`) values('{}','{}') ".format(image_title,image_url))
30 print('正在保存 %s'%image_title)
31 db.commit()
32 # range 范围 1<=X<1000
33 for i in range(1,1001):
34 print('第{}页'.format(i))
35 getImagesList(i)
效果图

使用的框架是Flask
1 from flask import Flask
2 from flask import render_template
3 from flask import request
4 import pymysql
5 # 404 页面未找到
6 app = Flask(__name__)
7 # 装饰器
8 @app.route('/') # route 路由
9 def index():
10 # return "hello world"
11 return render_template('index.html')
12 @app.route('/search')
13 def search():
14 # 接收用户关键字
15 keyword = request.args.get('kw')
16 count = request.args.get('count')
17 cursor.execute("select * from images where name like '%{}%'".format(keyword))
18 data = cursor.fetchmany(int(count))
19 return render_template('index.html',images = data)
20 # 程序的入口
21 if __name__ == '__main__':
22 db = pymysql.connect(host='127.0.0.1', port=3306, db='db', user='root', passwd='root', charset='utf8',cursorclass = pymysql.cursors.DictCursor)
23 # 创建游标
24 cursor = db.cursor()
25 # 调试模式
26 # port 端口号 默认5000
27 app.run(debug=True,port=8000)
运行效果图

很多初学者,对Python的概念都是模糊不清的,Python能做什么,学的时候,该按照什么线路去学习,学完往哪方面发展,想深入了解,详情可以点击有道云笔记链接了解:http://note.youdao.com/noteshare?id=e4fa02e7b56d7909a27674cdb3da08aa
来源:https://www.cnblogs.com/ITbiancheng/p/12114252.html