温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python怎么开发个人专属的表情包网站

发布时间:2021-11-25 15:01:27 来源:亿速云 阅读:128 作者:iii 栏目:大数据

这篇文章主要介绍“Python怎么开发个人专属的表情包网站”,在日常操作中,相信很多人在Python怎么开发个人专属的表情包网站问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python怎么开发个人专属的表情包网站”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

爬取表情包存入数据库

环境: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)

效果图

Python怎么开发个人专属的表情包网站

网站开发

使用的框架是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)

到此,关于“Python怎么开发个人专属的表情包网站”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI