Flask中实现使用Mongodb分页的程序逻辑
实现的逻辑如下:
page = int(request.args.get('page')) if request.args.get('page') else 1 pagesize = 20 prev_page = page - 1 if page - 1 else 1 next_page = page + 1 # 关键字查询 keywords = request.args.get('keywords') start_date = request.args.get('start_date') end_date = request.args.get('end_date') params = [] params_query = {} if keywords: params.append({'title':{'$regex':'^%s' %keywords }}) params_query['keywords'] = keywords if start_date and end_date: start = '%s 00:00:00' % start_date end = '%s 23:59:59' % end_date start = int(time.mktime(time.strptime(start,'%Y-%m-%d %H:%M:%S'))) end = int(time.mktime(time.strptime(end,'%Y-%m-%d %H:%M:%S'))) params.append({'create_time':{'$gt':start}}) params.append({'create_time':{'$lte':end}}) params_query['start_date'] = start_date params_query['end_date'] = end_date if params: # if len(params) == 1: # where = params # else: where = {'$and':params} itunes_sum = mongo.db.itunes.find(where).count() itunes = mongo.db.itunes.find(where).skip((page - 1) * pagesize).sort('create_time',-1).limit(pagesize) else: itunes_sum = mongo.db.itunes.count() itunes = mongo.db.itunes.find().skip((page - 1) * pagesize).sort('create_time',-1).limit(pagesize) return render_template( 'front/spider_itunes.html', itunes=itunes, itunes_sum=itunes_sum, title=u"iTunes爬虫", prev_page=prev_page, next_page=next_page, params=params_query )
mongodb还是别扭点,常用就好了。
版权声明
由 durban创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 博客( https://www.gowhich.com ),版权所有,侵权必究。
本文永久链接: https://www.gowhich.com/blog/623
版权声明
由 durban创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 Gowhich博客( https://www.gowhich.com ),版权所有,侵权必究。
本文永久链接: https://www.gowhich.com/blog/623