|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Tom.Error 于 2019-8-18 18:17 编辑
下面代码为完整代码,下载图片下下来正常,可下载MP3时,下载下来就没法打开,大佬可以看看哪里出问题大佬么 谢谢
- import requests
- import os
- import threading
- class Mythread(threading.Thread):
- def __init__(self,url,startpos,endpos,f):
- super(Mythread,self).__init__()
- self.url = url
- self.startpos = startpos
- self.endpos = endpos
- self.fd = f
- def download(self):
- print('start thread:%s at %s'%(self.getName(), self.startpos))
- headers = {'Range':'bytes=%s-%s'%(self.startpos,self.endpos)}
- res = requests.get(self.url,headers=headers)
- self.fd.seek(self.startpos)
- self.fd.write(res.content)
- print('Stop shread:%s at %s'%(self.getName(), self.startpos))
- self.fd.close()
- def run(self):
- self.download()
- def Download(url, name, type_, path='file'):
- content_size = int(requests.get(url).headers['content-length']) #大小
- threadnum = 3
- threading.BoundedSemaphore(threadnum) # 允许线程个数
- step = content_size // threadnum
- file_list = []
- start = 0
- end = -1
- path = './' + str(path) + '/'
- isExists_book = os.path.exists(path)
- if not isExists_book:
- os.makedirs(path)
- print('创建%s成功' % path)
- file_path = path + name + '.' + type_
- with open(file_path, 'wb+') as f:
- fileno = f.fileno() #返回一个整型的文件描述符,可用于底层操作系统的 I/O 操作
- while end < content_size-1:
- start = end + 1
- end = start + step -1
- if end > content_size:
- end = content_size
- print('start: %s,end: %s'%(start,end))
- dup = os.dup(fileno) #复制文件句柄
- fd = os.fdopen(dup, 'rb+', -1)
- t = Mythread(url,start,end,fd)
- t.start()
- file_list.append(t)
- for i in file_list:
- i.join()
- if __name__ == '__main__':
- url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566106547294&di=cc2aedbaa01adf93e26afd14968f838e&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fb151f8198618367aa7f3cc7424738bd4b31ce525.jpg'
- #url = 'http://music.163.com/song/media/outer/url?id=189602.mp3'
- name = '111'
- type_ = 'jpg'
- path = 'img'
- Download(url,name,type_,path)
复制代码
至少你代码中的MP3地址被服务器判断为爬虫给ban了, 给header加上user-agent
|
-
|