es+flask搜索小項(xiàng)目實(shí)現(xiàn)分頁+高亮的示例代碼
環(huán)境
- 前端:html,css,js,jQuery,bootstrap
- 后端:flask
- 搜索引擎:elasticsearch
- 數(shù)據(jù)源:某某之家
項(xiàng)目展示
項(xiàng)目目錄
主要源碼
獲取數(shù)據(jù)源并寫入es
from lxml import etree from concurrent.futures import ThreadPoolExecutor from elasticsearch import Elasticsearch from elasticsearch import helpers import requests headers = { '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/h3/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()
視圖函數(shù)
from flask import Blueprint from flask import request from flask import render_template from flask import jsonify from web.ext import es from pprint import pprint search_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": "<span style='color: red'>" , "post_tags": "</span>" , "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)
前端
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>General Search</title> <link rel="stylesheet" href="{{ url_for('static',filename='css/bootstrap.min.css') }}"> <script src="{{ url_for('static',filename='js/jQuery.js') }}"></script> <script src="{{ url_for('static',filename='js/bootstrap.min.js') }}"></script> <style> .title{ font-size: 25px; font-weight: 10; } .result{ color: red; } </style> </head> <body> <div> <nav class="navbar navbar-default"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#" style="margin-left: 100px">General Search</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> </ul> <div class="navbar-form navbar-left"> <div class="form-group"> <input type="text" class="form-control" placeholder="" id="content" style="width: 800px;margin-left: 200px"> </div> </div> <button class="btn btn-primary pull-right" id="submit" style="margin-top: 8px;width: 100px;">搜 索</button> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> </div> <div class="container"> General為您找到相關(guān)結(jié)果約<span id='count' class="result">0</span>個(gè) <hr> </div> <div class="container" id="tags"> </div> <div class="text-center"> <nav aria-label="Page navigation"> <ul class="pagination" id="page"> </ul> </nav> </div> <script> function getData(current) { let content=$('#content').val() let tags=$('#tags') $('#count').text() tags.empty() $.ajax({ url:"", type:'post', data:{ 'content':content, 'current':current }, dataType:'JSON', success:function (res){ let length=res.total let need_page=res.need_page $('#count').text(length) $.each(res.res,function (index,value){ let source=value._source let title=source.title let highlight=value.highlight if (highlight) { title=highlight.title } let div=` <div> <p><a href="${source.url}" target="_blank"> <span class="title">${title}</span> <br> <img src="${source.img}" alt=""> </a></p> <p>${source.desc}</p> <hr> </div> ` tags.append(div) }) $('#page').empty() for(let i=0;i<need_page;i++) { let tmp=i+1 if (current==i) { $('#page').append('<li class="active"><span class="changePage">'+tmp+'</span></li>') } else { $('#page').append('<li><span class="changePage">'+tmp+'</span></li>') } } } }) } $('#submit').click(function (){ getData(0) }) $('#page').on('click','li',function (){ getData($(this).text()-1) }) </script> </body> </html>
app配置
from flask import Flask from flask_session import Session from web.ext import db from .search.search import search_bp from flask_script import Manager from flask_migrate import Migrate, MigrateCommand def 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
總結(jié)
對比django,flask最后選用自由度較大的flask。
集合flask-script,flask_sqlalchemy,flask-migrate加快開發(fā)。
寫一個(gè)小demo深化了對es接口的理解。
到此這篇關(guān)于es+flask搜索小項(xiàng)目實(shí)現(xiàn)分頁+高亮的示例代碼的文章就介紹到這了,更多相關(guān)es flask分頁+高亮內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python開發(fā)寶典CSV JSON數(shù)據(jù)處理技巧詳解
在Python中處理CSV和JSON數(shù)據(jù)時(shí),需要深入了解這兩種數(shù)據(jù)格式的讀取、寫入、處理和轉(zhuǎn)換方法,下面將詳細(xì)介紹如何在Python中處理CSV和JSON數(shù)據(jù),并提供一些示例和最佳實(shí)踐2023-11-11利用Python實(shí)現(xiàn)智能合約的示例詳解
智能合約是一種由計(jì)算機(jī)程序編寫的自動化合約,它可以在沒有第三方干預(yù)的情況下執(zhí)行交易和契約條款。這篇文章主要介紹了如何利用Python實(shí)現(xiàn)智能合約,需要的可以參考一下2023-04-04python selenium UI自動化解決驗(yàn)證碼的4種方法
本篇文章主要介紹了python selenium UI自動化解決驗(yàn)證碼的4種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01解決Python2.7中IDLE啟動沒有反應(yīng)的問題
今天小編就為大家分享一篇解決Python2.7中IDLE啟動沒有反應(yīng)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11python通過偽裝頭部數(shù)據(jù)抵抗反爬蟲的實(shí)例
下面小編就為大家分享一篇python通過偽裝頭部數(shù)據(jù)抵抗反爬蟲的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05Python把png轉(zhuǎn)成jpg的項(xiàng)目實(shí)踐
本文主要介紹了Python把png轉(zhuǎn)成jpg的項(xiàng)目實(shí)踐,可以使用PIL庫來將PNG圖片轉(zhuǎn)換為JPG格式,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02