多线程下载
Posted notfind
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程下载相关的知识,希望对你有一定的参考价值。
多线程下载
梨视频下载为例
要点:
1.获取所有视频的url,定义下载函数getdata
2.将下载数据保存到列表,定义数据保存函数savefunc
3.from multiprocessing.dummy import Pool
? pool = Pool(4)
? pool.get(getdata,url)
? pool.map(savefunc,datalist)
import requests,re
from multiprocessing.dummy import Pool
from lxml import etree
from random import randint
target_url = 'https://www.pearvideo.com'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
}
response = requests.get(url = target_url,headers = headers)
ht = response.text
tree = etree.HTML(ht)
link_list = tree.xpath("//*[@id = 'actRecommendCont'][1]//a[@class = 'actcont-detail actplay']/@href")
videoUrl = []
for link in link_list:
detail = 'https://www.pearvideo.com/' + link
response1 = requests.get(url = detail,headers=headers)
ht = response1.text
reg = 'var contId.*?srcUrl="(.*?)"'
link = re.findall(reg,ht,re.S)[0]
videoUrl.append(link)
def getVideoDate(url):
return requests.get(url = url,headers = headers).content
pool = Pool(4)
videoData_list = pool.map(getVideoDate,videoUrl)
def saveVideo(VD):
fn = int(randint(1,999))
with open('./%s.mp4' % fn,'wb') as f:
f.write(VD)
print('视频%s下载完毕' % fn)
pool.map(saveVideo,videoData_list)
以上是关于多线程下载的主要内容,如果未能解决你的问题,请参考以下文章