网易云音乐搜索引擎 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()
python运行这个server.py代码之后就会监听8080端口,那么你只需要打开 127.0.0.1:8080 就可以看到相应文件夹下面的内容


前台网页: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_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+"
" 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

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,就是代表搜索歌手阿宝的歌曲,如果点击歌曲,就可以增加这首歌的排名优先级,并新打开网易云音乐对应歌曲的页面


所有代码及运行说明已上传,点击这里下载

原创粉丝点击