python 念聽網mp3多執行緒多工下載
阿新 • • 發佈:2018-11-09
#coding=utf-8
import requests
import threading
import re
class downloader:
# 建構函式
def __init__(self,downloadurl):
# 設定url
#downloadurl='http://file.ting55.com/down/2016/10/8030/15.mp3?s=nb2ZbTb7gOGbMQ4JybtcpQ&e=1490250302&n=五大賊王1落馬青雲_15.mp3'
self.url = downloadurl.split('&n=' )[0]
# 設定執行緒數
self.num = 8
# 檔名從url最後取
self.name = downloadurl.split('&n=')[1]
# 用head方式去訪問資源
r = requests.head(self.url)
# 取出資源的位元組數
self.total = int(r.headers['Content-Length'])
print 'total is %s' % (self.total)
def get_range (self):
ranges = []
# 比如total是50,執行緒數是4個。offset就是12
offset = int(self.total / self.num)
for i in range(self.num):
if i == self.num - 1:
# 最後一個執行緒,不指定結束位置,取到最後
ranges.append((i * offset, ''))
else:
# 沒個執行緒取得區間
ranges.append((i * offset, (i + 1) * offset))
# range大概是[(0,12),(12,24),(25,36),(36,'')]
return ranges
def download(self, start, end):
headers = {'Range': 'Bytes=%s-%s' % (start, end), 'Accept-Encoding': '*'}
# 獲取資料段
res = requests.get(self.url, headers=headers)
# seek到指定位置
print '%s:%s download success' % (start, end)
self.fd.seek(start)
self.fd.write(res.content)
def run(self):
# 開啟檔案,檔案物件存在self裡
self.fd = open(r'E:/PycharmProjects/download/output/'+self.name, 'w')
thread_list = []
n = 0
for ran in self.get_range():
start, end = ran
print 'thread %d start:%s,end:%s' % (n, start, end)
n += 1
# 開執行緒
thread = threading.Thread(target=self.download, args=(start, end))
thread.start()
thread_list.append(thread)
for i in thread_list:
# 設定等待
i.join()
print 'download %s load success' % (self.name)
self.fd.close()
def getPage(url):
html=requests.get(url)
content=html.text
#print content
return content
def getDownloadUrls(page_content):
pattern=re.compile(r'<a href="(.*?)".*?>',re.S)
downloadUrls=re.findall(pattern,page_content)
return downloadUrls[-11]
if __name__ == '__main__':
for i in xrange(1, 61):
url = "http://ting55.com/down/8030-%d" % i
page_content = getPage(url)
downloadLink = getDownloadUrls(page_content)
# 新建例項
down = downloader(downloadLink)
# 執行run方法
down.run()