Python 抓取虾米歌曲名

# -*- coding:utf8 -*-
import httplib
def txt_wrap_by(start_str, end, html):
    start = html.find(start_str)
    if start >= 0:
        start += len(start_str)
        end = html.find(end, start)
        if end >= 0:
            return html[start:end].strip()

def getones(sid):
   host = 'www.xiami.com'
   port = httplib.HTTP_PORT
   url = '/widget/xml-multi/uid/0/sid/'+sid+',/width/290/height/250/mainColor/cccccc/backColor/ffffff/autoplay/1'
   hl = httplib.HTTPConnection(host, port)
   hl.request('GET', url)
   resp = hl.getresponse()
   respHtml = resp.read()
   respHtml = unicode(respHtml, "utf8").encode("utf8")
   songname = txt_wrap_by("<song_name><![CDATA[","]]></song_name>",respHtml)
   #l.close()
  #print respHtml

   return songname

if __name__=="__main__":	
  for x in xrange(1,10):
    print(getones(str(x)))
       输出中文乱码了,求解 --,

发表评论