python 網頁抓取(使用代理服務器),解析一例


#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')

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章