偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python多線程下載有聲小說(shuō)

開發(fā) 后端
這里不會(huì)漫無(wú)目的的取爬取一個(gè)網(wǎng)站的所有鏈接,而是給定一個(gè)小說(shuō),比方說(shuō)我要下載小說(shuō)《童年》,我會(huì)在我聽評(píng)書網(wǎng)上找到該小說(shuō)的主頁(yè)然后用程序下載所有mp3音頻,具體做法見下面代碼。

有經(jīng)驗(yàn)的老鳥都(未婚的)會(huì)在公司附近租房,免受舟車勞頓之苦的同時(shí)節(jié)約了大把時(shí)間;也有些人出于某種原因需要每天披星戴月地游走于公司與家之間,很不幸俺就是這其中一員。由于家和公司離得比較遠(yuǎn),我平時(shí)在公交車上的時(shí)間占據(jù)了工作時(shí)間段的1/4,再加上杭州一向有中國(guó)的拉斯維加斯之稱(堵城),每每堵起來(lái),哥都能想象自己成為變形金剛。這段漫長(zhǎng)時(shí)間我想作為每個(gè)程序猿來(lái)說(shuō)是無(wú)法忍受的,可是既然短時(shí)間無(wú)法改變生存的現(xiàn)狀,咱就好好利用這段時(shí)間吧。所以,我特地買了大屏幕的Note II 以便看pdf,另外耳朵也不能閑著,不過(guò)咱不是聽英語(yǔ)而是聽小說(shuō),我在讀書的時(shí)候就喜歡聽廣播,特別是說(shuō)書、相聲等,所以我需要大量的有聲小說(shuō),現(xiàn)在網(wǎng)上這些資源多的很,但是下載頁(yè)記為麻煩,為了掙取更多的流量和廣告點(diǎn)擊,這些網(wǎng)站的下載鏈接都需要打開至少兩個(gè)以上的網(wǎng)頁(yè)才能找到真正的鏈接,甚是麻煩,為了節(jié)省整體下載時(shí)間,我寫了這個(gè)小程序,方便自己和大家下載有聲小說(shuō)(當(dāng)然,還有任何其他類型的資源)

先說(shuō)明一下,我不是為了爬很多資料和數(shù)據(jù),僅僅是為了娛樂(lè)和學(xué)習(xí),所以這里不會(huì)漫無(wú)目的的取爬取一個(gè)網(wǎng)站的所有鏈接,而是給定一個(gè)小說(shuō),比方說(shuō)我要下載小說(shuō)《童年》,我會(huì)在我聽評(píng)書網(wǎng)上找到該小說(shuō)的主頁(yè)然后用程序下載所有mp3音頻,具體做法見下面代碼,所有代碼都在模塊crawler5tps中:

1. 先設(shè)定一下start url 和保存文件的目錄

  1. #-*-coding:GBK-*-  
  2.  import urllib,urllib2  
  3.  import re,threading,os  
  4.  baseurl = 'http://www.5tps.com' #base url   
  5.  down2path = 'E:/enovel/'        #saving path  
  6.  save2path = ''                  #saving file name (full path) 

2. 從start url 解析下載頁(yè)面的url

  1. def parseUrl(starturl):  
  2.      '''''  
  3.      parse out download page from start url.  
  4.      eg. we can get 'http://www.5tps.com/down/8297_52_1_1.html' from 'http://www.5tps.com/html/8297.html'  
  5.      ''' 
  6.      global save2path  
  7.      rDownloadUrl = re.compile(".*?<A href=\'(/down/\w+\.html)\'.*"#find the link of download page  
  8.      #rTitle = re.compile("<TITILE>.{4}\s{1}(.*)\s{1}.*</TITLE>")  
  9.      #<TITLE>有聲小說(shuō) 悶騷1 播音:劉濤 全集</TITLE>  
  10.      f = urllib2.urlopen(starturl)  
  11.      totalLine =  f.readlines()  
  12.        
  13.     ''''' create the name of saving file ''' 
  14.      title = totalLine[3].split(" ")[1]  
  15.      if os.path.exists(down2path+title) is not True:  
  16.          os.mkdir(down2path+title)  
  17.          save2path = down2path+title+"/" 
  18.        
  19.      downUrlLine = [ line for line in totalLine if rDownloadUrl.match(line)]  
  20.      downLoadUrl = [];  
  21.      for dl in downUrlLine:  
  22.          while True:  
  23.              m = rDownloadUrl.match(dl)  
  24.              if not m:  
  25.                  break 
  26.              downUrl = m.group(1)  
  27.              downLoadUrl.append(downUrl.strip())  
  28.              dl = dl.replace(downUrl,'')  
  29.      return downLoadUrl 

3. 從下載頁(yè)面解析出真正的下載鏈接

  1. def getDownlaodLink(starturl):  
  2.      '''''  
  3.      find out the real download link from download page.  
  4.      eg. we can get the download link 'http://180j-d.ysts8.com:8000/人物紀(jì)實(shí)/童年/001.mp3?\  
  5.      1251746750178x1356330062x1251747362932-3492f04cf54428055a110a176297d95a' from \  
  6.      'http://www.5tps.com/down/8297_52_1_1.html'  
  7.      ''' 
  8.      downUrl = []  
  9.      gbk_ClickWord = '點(diǎn)此下載' 
  10.      downloadUrl = parseUrl(starturl)  
  11.      rDownUrl = re.compile('<a href=\"(.*)\"><font color=\"blue\">'+gbk_ClickWord+'.*</a>'#find the real download link  
  12.      for url in downloadUrl:  
  13.          realurl = baseurl+url  
  14.          print realurl  
  15.          for line in urllib2.urlopen(realurl).readlines():  
  16.              m = rDownUrl.match(line)  
  17.              if m:  
  18.                  downUrl.append(m.group(1))  
  19.      
  20.      return downUrl 

4. 定義下載函數(shù)

  1. def download(url,filename):  
  2.      ''''' download mp3 file ''' 
  3.      print url  
  4.      urllib.urlretrieve(url, filename) 

5. 創(chuàng)建用于下載文件的線程類

  1. class DownloadThread(threading.Thread):  
  2.      ''''' dowanload thread class ''' 
  3.      def __init__(self,func,savePath):  
  4.          threading.Thread.__init__(self)  
  5.          self.function = func  
  6.          self.savePath = savePath  
  7.        
  8.      def run(self):  
  9.          download(self.function,self.savePath) 

6. 開始下載

  1. if __name__ == '__main__':  
  2.      starturl = 'http://www.5tps.com/html/8297.html' 
  3.      downUrl = getDownlaodLink(starturl)  
  4.      aliveThreadDict = {}        # alive thread  
  5.      downloadingUrlDict = {}     # downloading link  
  6.    
  7.      i = 0;  
  8.      while i < len(downUrl):  
  9.          ''''' Note:我聽評(píng)說(shuō)網(wǎng) 只允許同時(shí)有三個(gè)線程下載同一部小說(shuō),但是有時(shí)受網(wǎng)絡(luò)等影響,\  
  10.                          為確保下載的是真實(shí)的mp3,這里將線程數(shù)設(shè)為2 ''' 
  11.          while len(downloadingUrlDict)< 2 :  
  12.              downloadingUrlDict[i]=i  
  13.              i += 1 
  14.          for urlIndex in downloadingUrlDict.values():  
  15.              #argsTuple = (downUrl[urlIndex],save2path+str(urlIndex+1)+'.mp3')  
  16.              if urlIndex not in aliveThreadDict.values():  
  17.                  t = DownloadThread(downUrl[urlIndex],save2path+str(urlIndex+1)+'.mp3')  
  18.                  t.start()  
  19.                  aliveThreadDict[t]=urlIndex  
  20.          for (th,urlIndex) in aliveThreadDict.items():  
  21.              if th.isAlive() is not True:  
  22.                  del aliveThreadDict[th] # delete the thread slot  
  23.                  del downloadingUrlDict[urlIndex] # delete the url from url list needed to download   
  24.        
  25.      print 'Completed Download Work' 

這樣就可以了,讓他盡情的下吧,咱還得碼其他的項(xiàng)目去,哎 >>>

等下了班copy到Note中就可以一邊聽小說(shuō)一邊看資料啦,***附上源碼

原文鏈接:http://www.cnblogs.com/wuren/archive/2012/12/24/2831100.html

責(zé)任編輯:張偉 來(lái)源: 博客園
相關(guān)推薦

2015-02-03 15:06:23

android多線程下載

2014-12-31 15:42:21

Android多線程軟件下載

2011-05-31 13:29:40

Android 多線程

2010-02-01 17:25:09

Python多線程

2023-10-06 23:06:01

多線程Python

2009-03-12 10:52:43

Java線程多線程

2010-02-01 17:18:23

Python多線程環(huán)境

2021-08-12 14:33:20

Python多線程編程

2010-04-27 08:28:03

2010-03-18 16:02:09

python 多線程

2010-03-03 17:44:07

Python多線程

2010-03-10 08:54:49

Python多線程

2010-03-10 18:32:45

Python多線程

2023-10-18 15:19:56

2022-03-09 17:01:32

Python多線程多進(jìn)程

2024-10-16 09:34:50

2013-07-16 10:12:14

iOS多線程多線程概念多線程入門

2023-06-05 07:56:10

線程分配處理器

2023-06-06 08:17:52

多線程編程Thread類

2010-01-21 11:27:30

linux多線程機(jī)制線程同步
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)