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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值