python 念聽網mp3多線程多任務下載


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

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com