查找歌词的python的脚本

Python/PHP/Perl 开发与设计
回复
头像
oneleaf
论坛管理员
帖子: 10229
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04
送出感谢: 7 次
接收感谢: 103 次

查找歌词的python的脚本

#1

帖子 oneleaf » 2006-05-25 13:08

代码: 全选

#!/usr/bin/python
# -*- coding: gbk -*-
# Copyright (c) 2006 UbuntuChina <http://www.ubuntu.org.cn>
# License: GPLv2
# Author: oneleaf <oneleaf AT gmail.com> 

import urllib
import BaseHTTPServer,SocketServer
import httplib
import re

htmlhead='''
<html>
<head>
<META content="text/html; charset=gbk" http-equiv=Content-Type>
<title>MP3 Search</title>
<style type="text/css">
td.row1	{ background-color: #fff8f2; }
td.row2	{ background-color: #f8f5ee; }
</style>
</head>
<body>
<form>
<input name="word" value="%s">&nbsp;<input value="Search"  type="submit">
</form>
'''
htmlfood='''
</body>
</html>
'''

def get(self,word):
    conn = httplib.HTTPConnection('mp3.baidu.com')
    url='/m?f=ms&tn=baidump3lyric&ct=150994944&lf=2&rn=10&word='+word+'&lm=-1'
    conn.request("GET",url)
    response = conn.getresponse()
    html=response.read()
    expression='<div style="padding-left:10px;line-height:20px;padding-top:1px">.*\n.*'
    lineno=0
    listSentence = re.findall(expression, html)
    self.wfile.write('<table align="center" width="100%" cellpadding="1" cellspacing="2">')
    while lineno<len(listSentence):
        self.wfile.write('<tr>')
        self.wfile.write('<td class=row'+str(lineno%2+1)+' align="left" width="100%">')
        self.wfile.write(listSentence[lineno][64:-1])
        self.wfile.write('</td>')
        self.wfile.write('</tr>')
        lineno+=1
    self.wfile.write('</table>')

class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_head()
        if self.path.find('word')==-1:
            self.wfile.write(htmlhead % '')
            self.wfile.write(htmlfood)
        else:
            word=self.path[7:]
            self.wfile.write(htmlhead % urllib.unquote(word))
            get(self,word)
            self.wfile.write(htmlfood)

    def send_head(self):
        self.send_response(200)
        self.send_header("content-type","text/html")
        self.end_headers()

class myWebServer(SocketServer.ThreadingMixIn,BaseHTTPServer.HTTPServer): pass

if __name__ == "__main__":
    server = myWebServer(('', 8080), RequestHandler)
    server.serve_forever()
lvscar
帖子: 59
注册时间: 2005-08-21 0:41
来自: 北京
送出感谢: 0
接收感谢: 0
联系:

#2

帖子 lvscar » 2006-05-28 11:12

谢谢。。。
回复

回到 “Python/Php/Perl”