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.texttree = 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()
视图函数
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 = 10current = 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) + 1data = https://www.it610.com/article/{'res': new_res[current * size:current * size + size],'need_page': need_page,'total': total}return jsonify(data)
前端
General Search - 锐客网 .title{font-size: 25px; font-weight: 10; }.result{color: red; }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
总结 对比django,flask最后选用自由度较大的flask。
集合flask-script,flask_sqlalchemy,flask-migrate加快开发。
写一个小demo深化了对es接口的理解。
【es+flask搜索小项目实现分页+高亮的示例代码】到此这篇关于es+flask搜索小项目实现分页+高亮的示例代码的文章就介绍到这了,更多相关es flask分页+高亮内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 数据结构|数据结构 Java数据结构 --- 二叉搜索树
- javascript|自己用js做一个搜索栏,超级简单
- 《寒假算法集训》|《寒假算法集训》(专题十九)广度优先搜索
- Java学习|Java小项目 零钱通
- 端智能在大众点评搜索重排序的应用实践
- 基于 Kafka 的实时数仓在搜索的实践应用
- 基于|基于 Kafka 的实时数仓在搜索的实践应用
- 一文搞懂|一文搞懂 C 指针(数组指针、函数指针)
- 大厂偏爱的Agent技术究竟是个啥
- 算法系列|【算法基础1】舍友课间上了个厕所,回来就告诉我他掌握了二分查找【内附搜索模板】