媒体大数据词汇表查询(Python+Flask+MySQL)

2021/6/10 2:22:48

本文主要是介绍媒体大数据词汇表查询(Python+Flask+MySQL),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Python+Flask+MySQL词汇表查询

      • MySQL导入词汇表
      • 代码实现Python的Flask应用
      • HTML前端代码
      • 运行结果

MySQL导入词汇表

在这里插入图片描述

代码实现Python的Flask应用

import pymysql
from flask import Flask,render_template,request

app=Flask(__name__)

@app.route('/')
def english():
    db = pymysql.connect(host="127.0.0.1",user="root",password="Ppyh0615",database="engword",port=3306,charset="utf8")
    cursor = db.cursor()
    try:
        sql="select * from map_enword limit 200"
        cursor.execute(sql)
        rs = cursor.fetchall()
        rs = list(rs)
        print(rs)
    except:
        rs = 'db-error'
        print('py-db-error')
    db.close()
    return render_template('english.html', rs=rs)

@app.route('/search')
def search():
    wanted = request.args.get("wanted", type=str)
    if wanted == None:
        wanted = 'pineapple'
    db = pymysql.connect(host="127.0.0.1",user="root",password="Ppyh0615",database="engword",port=3306,charset="utf8")
    cursor = db.cursor()
    try:
        sql = "select * from map_enword where english like '%" + wanted + "%'"
        cursor.execute(sql)
        rs = cursor.fetchall()
        rs = list(rs)
        print(rs)
    except:
        rs = 'db-error'
        print('py-db-error')

    db.close()
    return render_template('english.html',rs=rs)

if __name__=='__main__':
    app.run()

HTML前端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>英语四六级词表</title>
</head>
<body>
<h1>英语四六级词表</h1>

<form action="/search">
        <input type="text" name="wanted" value="" default="apple">
        <input type="submit" value="搜索">
</form>

{% for r in rs %}
{{r}}<br>
{% endfor %}
</body>
</html>

运行结果

在这里插入图片描述



这篇关于媒体大数据词汇表查询(Python+Flask+MySQL)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程