#-*- coding:utf-8 -*- import web import json import requests #from web.contrib.template import render_jinja
#render=render_jinja('templates',encoding='utf-8')用jinja2渲染 urls=( r'/index','Index', r'/search','Search' )
#db=web.database(dbn='mysql',host='',user='',pw='',port=,db='',charset-'utf8')
#db.query|insert|update|delete render=web.template.render('templates')#webpy自带的渲染 def geturl(name): #request=requests.post('http://music.163.com/api/search/get/?type=1&s=%s&limit=10'%name,headers={'appver':'2.0.2','referer':'http://music.163.com'}) try: result=requests.get('http://s.music.163.com/search/get/?type=1&s=%s&limit=10'%name,headers={'appver':'2.0.2','referer':'http://music.163.com'}) result=json.loads(result.content.decode('utf-8')) if result.get('result'): return result['result']['songs'][0]['album']['picUrl'],result['result']['songs'][0]['audio'] else: return 'static/12.jpg','static/lizhi.mp3' except Exception as e: print e class Index(object): def GET(self): return render.a() class Search(object): def GET(self): text=web.input().get('search',None) imgurl,mp3url=geturl(text) return render.music(imgurl,mp3url) if __name__=='__main__': #print geturl('love') web.application(urls,globals()).run()
#music.html
$def with(imgurl,mp3url,data) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div style='background-image:url($:imgurl);800px;height:600px;background-repeat:no-repeat'> <div style='300px;height:32px;margin:0 auto;line-height:600px'> <!--audio controls> <source src="$:mp3url"> </audio-->
!--$for a in data:
<p> $:a.title </p>--!> <audio src="$:mp3url" controls="controls"> </audio> </div> </div> </body> </html>