python 网页抓取(使用代理服务器),解析一例

来源:百度文库 编辑:神马文学网 时间:2024/04/28 20:12:33
Java代码
  1. #coding:utf-8  
  2. '''   
  3. Created on 2009-7-15  
  4.   
  5. @author: Administrator   
  6. '''   
  7. import urllib2   
  8. import newhttplib   
  9. import lxml.html as x     
  10.   
  11. def getmusic(num,soc=None):   
  12.     s=num.split(':')   
  13.     s[0]=s[0].strip()   
  14.     s[1]=s[1].strip()   
  15.     s[2]=s[2].strip()   
  16.     h1 = newhttplib.HTTPConnection('10.0.0.172',80)   
  17.     h1.auto_open = 0  
  18.     h1.connect(soc)   
  19.     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])    
  20.     h1.request("GET",url)    
  21.     r1 = h1.getresponse()   
  22.     content=r1.read()   
  23.     doc=x.document_fromstring(content)     
  24.     alist=doc.xpath("//a")   
  25.     print s[0],s[1]   
  26.     for a in alist:   
  27.         c1= a.text_content()   
  28.         try:   
  29.             if c1.find(s[0])>=0 and c1.find(s[1])>=0:   
  30.                 print c1   
  31.                 print c1.find(s[0]),c1.find(s[1])   
  32.                 nexturl='http://218.200.160.29'+a.get('href')    
  33.                 h1.request("GET",nexturl)    
  34.                 r1 = h1.getresponse()   
  35.                 content=r1.read()   
  36.                 print content   
  37.                 doc=x.document_fromstring(content)     
  38.                 blist=doc.xpath("//a")   
  39.                 for b in blist:   
  40.                     bcontent=b.text_content()   
  41.                     if bcontent.find('高潮版')>=0 or bcontent.find('超长版')>=0:   
  42.                          print bcontent   
  43.                          bhref= b.get('href')   
  44.                          nnexturl='http://218.200.160.29'+bhref   
  45.                          h1.request("GET",nnexturl)    
  46.                          r1 = h1.getresponse()   
  47.                          ccontent=r1.read()   
  48.                          #print ccontent   
  49.                          doc=x.document_fromstring(ccontent)     
  50.                          clist=doc.xpath("//a")   
  51.                          for c in clist:   
  52.                              chref=c.get('href')   
  53.                              if chref.find('218.200.160.10')>=0:   
  54.                                  print chref   
  55.                                  sb2=s[2][11:]   
  56.                                  print sb2   
  57.                                  if chref.find(sb2)>=0:   
  58.                                      print u'找到匹配歌曲开始下载。。。'  
  59.                                      h1.request("GET",chref)    
  60.                                      r1 = h1.getresponse()   
  61.                                      print r1.status   
  62.                                      dcontent=r1.read()   
  63.                                      f=open('./test.mp3','wb')   
  64.                                      f.write(dcontent)   
  65.                                      f.close()   
  66.         except BaseException,e:   
  67.             print e   
  68.                
  69. if __name__ == '__main__':   
  70.     getmusic('相信:曾建军:600902000005714466')