Python 多线程爬取视频

20 篇文章 0 订阅
#coding:utf-8
import urllib 
import urllib2 
import requests
import random 
import uuid
import time
import sys
from threading import Thread

#img_url = "https://p.ssl.qhimg.com/dm/48_48_100/t017aee03b28107657b.jpg"




my_headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 6.3; Win64; x64) \
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
   'Referer':'https://www.bilibili.com/bangumi/play/ep250436',
}

img=[
   'http://xunleib.zuida360.com/1811/妖精的尾巴最终季-06.mp4',
   'http://xunleib.zuida360.com/1811/妖精的尾巴最终季-05.mp4',
   'http://vip.zuiku8.com/1810/妖精的尾巴最终季-04.mp4',
   'http://vip.zuiku8.com/1810/妖精的尾巴最终季-03.mp4',
   'http://vip.zuiku8.com/1810/妖精的尾巴最终季-02.mp4',
   'http://vip.zuiku8.com/1810/妖精的尾巴最终季-01.mp4'
]

def chunk_report(bytes_so_far, chunk_size, total_size,filename):
   percent = float(bytes_so_far) / total_size
   percent = round(percent*100, 2)
   if percent %1==0:
      sys.stdout.write("Downloaded %s ==> (%0.2f%%)\n" % 
           (filename, percent))

   if bytes_so_far >= total_size:
      sys.stdout.write('\n')

def chunk_read(response, url,chunk_size=8192, report_hook=None):
   total_size = response.info().getheader('Content-Length').strip()
   total_size = int(total_size)
   bytes_so_far = 0
   path_name=url.split("/")[-1]
   path_name=path_name.replace("\n","")
   path_name=path_name.decode("utf-8")
   print path_name
   with open("%s" % path_name, "wb") as f:
      while 1:
         chunk = response.read(chunk_size)
         f.write(chunk)
         f.flush() 
         bytes_so_far += len(chunk)
         if not chunk:
            break
         if report_hook:
            report_hook(bytes_so_far, chunk_size, total_size,path_name)
   return bytes_so_far

def down_load(img):
    while True:
        print img
        request =  urllib2.Request(url=img, headers=my_headers)
        response = urllib2.urlopen(request);
        chunk_read(response,img, report_hook=chunk_report)
        print "downloading with urllib  --->"


if __name__ == '__main__':

    for i in range(len(img)):
        t = Thread(target=down_load,args=(img[i],))
        t.start()

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值