es+flask搜索小项目实现分页和高亮的示例代码怎么写
发表于:2025-11-06 作者:千家信息网编辑
千家信息网最后更新 2025年11月06日,这篇文章的内容主要围绕es+flask搜索小项目实现分页和高亮的示例代码怎么写进行讲述,文章内容清晰易懂,条理清晰,非常适合新手学习,值得大家去阅读。感兴趣的朋友可以跟随小编一起阅读吧。希望大家通过这
千家信息网最后更新 2025年11月06日es+flask搜索小项目实现分页和高亮的示例代码怎么写
这篇文章的内容主要围绕es+flask搜索小项目实现分页和高亮的示例代码怎么写进行讲述,文章内容清晰易懂,条理清晰,非常适合新手学习,值得大家去阅读。感兴趣的朋友可以跟随小编一起阅读吧。希望大家通过这篇文章有所收获!
环境
前端:html,css,js,jQuery,bootstrap
后端:flask
搜索引擎:elasticsearch
数据源:某某之家
项目展示
项目目录
主要源码
获取数据源并写入es
from lxml import etreefrom concurrent.futures import ThreadPoolExecutorfrom elasticsearch import Elasticsearchfrom elasticsearch import helpersimport requestsheaders = { 'user-agent': 'ua'}es = Elasticsearch()if not es.indices.exists(index='car'): es.indices.create(index='car', mappings={ 'properties': { 'url': { 'type': 'text' }, 'img': { 'type': 'text' }, 'title': { 'type': 'text' }, 'desc': { 'type': 'text' } } })def task(url,page): res = requests.get(url, headers) text = res.text tree = etree.HTML(text) ul_list = tree.xpath('//ul[@class="article"]') actions = [] for ul in ul_list: li_list = ul.xpath('./li') for li in li_list: url = li.xpath('./a/@href'), img = li.xpath('./a/div/img/@src'), desc = li.xpath('./a/p/text()'), title = li.xpath('./a/h4/text()') if title: doc = { '_index': 'car', 'url': f'https:{url[0][0]}', 'img': img[0][0], 'desc': desc[0][0], 'title': title[0], } actions.append(doc) helpers.bulk(es, actions=actions) print(f'第{page}页完成!')def main(): with ThreadPoolExecutor() as pool: for i in range(1, 11): url = f'https://www.autohome.com.cn/all/{i}/' pool.submit(task, url=url,page=i)if __name__ == '__main__': main()视图函数
from flask import Blueprintfrom flask import requestfrom flask import render_templatefrom flask import jsonifyfrom web.ext import esfrom pprint import pprintsearch_bp = Blueprint('search', __name__, url_prefix='/search')@search_bp.route('/', methods=['get', 'post'])def search(): if request.method == 'GET': return render_template('search.html') elif request.method == 'POST': content = request.values.get('content') size = 10 current = int(request.values.get('current', '0')) if content: res = es.search(index='car', query={ 'match': { "title": content } }, highlight={ "pre_tags": "" , "post_tags": "" , "fields": { "title": {} } }, size=1000) else: res = es.search(index='car', query={ 'match_all': {} }, size=1000) new_res = res['hits']['hits'] total = int(res['hits']['total']['value']) need_page = (total // size) + 1 data = { 'res': new_res[current * size:current * size + size], 'need_page': need_page, 'total': total } return jsonify(data)前端
General Search General为您找到相关结果约0个
app配置
from flask import Flaskfrom flask_session import Sessionfrom web.ext import dbfrom .search.search import search_bpfrom flask_script import Managerfrom flask_migrate import Migrate, MigrateCommanddef create_app(): app = Flask(__name__) app.config.from_object('settings.DevelopmentConfig') app.register_blueprint(search_bp) Session(app) db.init_app(app) app = Manager(app) Migrate(app, db) app.add_command('db', MigrateCommand) return app感谢你的阅读,相信你对"es+flask搜索小项目实现分页和高亮的示例代码怎么写"这一问题有一定的了解,快去动手实践吧,如果想了解更多相关知识点,可以关注网站!小编会继续为大家带来更好的文章!
项目
搜索
代码
示例
高亮
内容
前端
数据
数据源
文章
篇文章
之家
兴趣
函数
引擎
搜索引擎
新手
易懂
更多
朋友
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
前端调取mysql数据库
怎样做云服务器
局域网访问数据库
深圳乐刻网络技术
delp数据库管理
三级拓扑图网络安全
数据库按季度取数
科技与生态互联网
成都摩宝网络技术公司
网络安全识人
网络安全测试 黑盒 白盒
凌重互联网科技
嘉兴是不是在开网络安全会议
小程序可以连数据库
直销游戏软件开发
安徽专业软件开发价格表
市政府网络安全建设
浙江以太坊网络技术有限公司
大数据检查复印机网络安全
百塔服务器管理
清空数据库表中的部分数据
软件开发类涉密资质乙级
服务器整流器
东莞管理软件开发哪家公司好
数据库查找所有姓王的学生
同城交友软件开发
打印服务器管理软件
武汉财务软件开发流程
停车信息数据库
徐州再出发网络技术公司