网易云音乐搜索引擎 python+whoosh---(3)界面
来源:互联网 发布:小区物业管理系统源码 编辑:程序博客网 时间:2024/06/08 14:44
网页和后台的通信采用ajax,参考文献:python处理ajax请求
后台代码:server.py
#!coding:utf8from BaseHTTPServer import HTTPServerfrom CGIHTTPServer import CGIHTTPRequestHandlerport=8080httpd=HTTPServer(('',port),CGIHTTPRequestHandler)print("Starting simple_http on port:"+str(httpd.server_port))httpd.serve_forever()
前台网页:index.html
网易云音乐搜索 歌曲 歌手 专辑 热门度
1,我使用了bootstrap的css样式表,你需要在第五行的href中换成你自己的位置
2,index.html的第16行代码表示后台运行"/cgi-bin/getuser.py"代码,这个代码点击搜索按钮saveUserInfo启动,搜索数据传入后台时起作用,getuser.py就是获取搜索结果的代码
3,index.html的第67行代码表示后台运行/cgi-bin/updt.py代码,这个代码在点击歌曲超链接update_hot_num函数启动时起作用,updt.py就是根据点击动作增加这首歌的优先级
4,组员信息在authors.html页面,第125行,是一个单纯的展示页面
getuser.py代码:
# -*- coding:utf-8 -*-import sysimport whoosh.index as indexfrom whoosh import columns, fields, index, sortingfrom whoosh.qparser import QueryParser,MultifieldParserimport cgi,cgitbimport urllib2reload(sys)sys.setdefaultencoding('utf8')form=cgi.FieldStorage()key_words=form.getvalue('key_words')sotype=form.getvalue('so_type')data=key_wordsix = index.open_dir(u"D:\课程学习\互联网信息搜索与挖掘\project\whoosh\indexdir")facet = sorting.FieldFacet("hot_num", reverse=True)searcher = ix.searcher()class switch(object): def __init__(self, value): self.value = value self.fall = False def __iter__(self): """Return the match method once, then stop""" yield self.match raise StopIteration def match(self, *args): """Indicate whether or not to enter a case suite""" if self.fall or not args: return True elif self.value in args: # changed for v1.5, see below self.fall = True return True else: return Falsedef p_results(results): str_2='''" str_1="%s\"%s\",%s %d %s"%("搜索",key_words.decode('utf-8'),"共找到",(index-1),"首单曲") print str_1 print str_2 print str_outprint "Content-type: text/html"###.decode('utf-8').encode('gbk')printfor case in switch(sotype): if case('s_hh'): qp = MultifieldParser(["music_name","artist_name","lyrics"], schema=ix.schema) q = qp.parse(key_words.decode('utf-8')) results = searcher.search(q, sortedby=facet) p_results(results) break if case('s_singer'): qp = QueryParser("artist_name", schema=ix.schema) q = qp.parse(key_words.decode('utf-8')) results = searcher.search(q, sortedby=facet) p_results(results) break if case('s_song'): qp = QueryParser("music_name", schema=ix.schema) q = qp.parse(key_words.decode('utf-8')) results = searcher.search(q, sortedby=facet) p_results(results) break if case('s_lyrics'): qp = QueryParser("lyrics", schema=ix.schema) q = qp.parse(key_words.decode('utf-8')) results = searcher.search(q, sortedby=facet) p_results(results) break歌曲 歌手 专辑 热门度 ''' str_out="" index=1 for hit in results: str_out=str_out+"%d%s%s%s%d"%(index,"http://music.163.com/#/song?id="+str(hit['music_id']),"_blank",hit['music_id'],hit['music_name'],"http://music.163.com/#/artist?id="+str(hit['artist_id']),"_blank",hit['artist_name'],"http://music.163.com/#/album?id="+str(hit['album_id']),"_blank",hit['album_name'],hit['comment_num']) index=index+1 str_out=str_out+"
updt.py代码:
# -*- coding:utf-8 -*-import sysimport whoosh.index as indexfrom whoosh.fields import Schema, ID, TEXTfrom whoosh import columns, fields, index, sortingfrom whoosh.qparser import QueryParser,MultifieldParserimport cgi,cgitbimport urllib2reload(sys)sys.setdefaultencoding('utf8')form=cgi.FieldStorage()music_id=form.getvalue('music_id')ix = index.open_dir(u"D:\课程学习\互联网信息搜索与挖掘\project\whoosh\indexdir")searcher = ix.searcher()stored_fields = searcher.document(music_id=int(music_id))if stored_fields: writer = ix.writer() writer.update_document(artist_id=stored_fields['artist_id'],artist_name=stored_fields['artist_name'],music_id=stored_fields['music_id'],music_name=stored_fields['music_name'],album_id=stored_fields['album_id'],album_name=stored_fields['album_name'],lyrics=stored_fields['lyrics'],comment_num=stored_fields['comment_num'],hot_num=(stored_fields['hot_num']+100)) writer.commit()
authors.html代码:
网易云音乐搜索 黄帅哥(队长)徐誉畅胡巧平016033910019016033910001016033910020
运行server.py之后可以监听8080,把index.html文件放在server.py同一个目录下,浏览器输入127.0.0.1:8080 ,选择index.html,就会打开搜索页面,在搜索框中输入“阿宝”两个汉字,选择歌手,点击搜索,此时会触发saveUserInfo函数把数据传到后台,后台运行getuser.py,就是代表搜索歌手阿宝的歌曲,如果点击歌曲,就可以增加这首歌的排名优先级,并新打开网易云音乐对应歌曲的页面
所有代码及运行说明已上传,点击这里下载
阅读全文
0 0
- 网易云音乐搜索引擎 python+whoosh---(3)界面
- 网易云音乐搜索引擎 python+whoosh---(2)搜索引擎
- 网易云音乐搜索引擎 python+whoosh---(1)背景和环境介绍
- 网易云音乐歌词python
- 网易云音乐锁屏界面实现
- Android仿网易云音乐播放界面
- 网易云音乐锁屏界面实现
- Android仿网易云音乐播放界面
- python whoosh
- Python 网易云音乐 MV 下载
- 【python】 网易云音乐 专辑图片+歌词
- Python 获取 网易云音乐热门评论
- 【python】免费下载网易云音乐会员音乐
- 其他技术 网易云音乐Mp3,通过网易官方搜索引擎获取mp3外链
- Android高仿网易云音乐播放界面
- 网易云音乐snap
- 网易云音乐爬虫
- 安装网易云音乐
- Flume架构介绍
- ffmpeg filter过滤器 基础实例及全面解析
- HTML canvas createLinearGradient() 方法
- MySQL-amoeba-读写分离
- python类之间方法互相调用
- 网易云音乐搜索引擎 python+whoosh---(3)界面
- 17、react之 reudx
- 利用IDEA搭建SSH框架时运行tomcat时出现Error listenerStart错误
- 接收RST回应的几种情况
- 组合算法
- 单例模式
- springAOP 实现事务的管理
- Intellij 快捷键
- 求int型正整数在内存中存储时1的个数