#coding:utf-8
'''
Created on 2009-7-15
@author: Administrator
'''
import urllib2
import newhttplib
import lxml.html as x
def getmusic(num,soc=None):
s=num.split(':')
s[0]=s[0].strip()
s[1]=s[1].strip()
s[2]=s[2].strip()
h1 = newhttplib.HTTPConnection('10.0.0.172',80)
h1.auto_open = 0
h1.connect(soc)
url='http://218.200.160.29/s3/i/app/search/musicResult.jsp?qd=1956&CH=12530-wap-lslb&v=1864&tplpath=/s3/i/qrc/&type=all&keyword='+urllib2.quote(s[0]+" "+s[1])
h1.request("GET",url)
r1 = h1.getresponse()
content=r1.read()
doc=x.document_fromstring(content)
alist=doc.xpath("//a")
print s[0],s[1]
for a in alist:
c1= a.text_content()
try:
if c1.find(s[0])>=0 and c1.find(s[1])>=0:
print c1
print c1.find(s[0]),c1.find(s[1])
nexturl='http://218.200.160.29'+a.get('href')
h1.request("GET",nexturl)
r1 = h1.getresponse()
content=r1.read()
print content
doc=x.document_fromstring(content)
blist=doc.xpath("//a")
for b in blist:
bcontent=b.text_content()
if bcontent.find('高潮版')>=0 or bcontent.find('超长版')>=0:
print bcontent
bhref= b.get('href')
nnexturl='http://218.200.160.29'+bhref
h1.request("GET",nnexturl)
r1 = h1.getresponse()
ccontent=r1.read()
#print ccontent
doc=x.document_fromstring(ccontent)
clist=doc.xpath("//a")
for c in clist:
chref=c.get('href')
if chref.find('218.200.160.10')>=0:
print chref
sb2=s[2][11:]
print sb2
if chref.find(sb2)>=0:
print u'找到匹配歌曲开始下载。。。'
h1.request("GET",chref)
r1 = h1.getresponse()
print r1.status
dcontent=r1.read()
f=open('./test.mp3','wb')
f.write(dcontent)
f.close()
except BaseException,e:
print e
if __name__ == '__main__':
getmusic('相信:曾建军:600902000005714466')
python 网页抓取(使用代理服务器),解析一例
最新推荐文章于 2024-07-24 08:56:18 发布