码迷,mamicode.com
首页 > 编程语言 > 详细

梨视频,进程池、线程池爬取

时间:2020-04-08 11:36:05      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:load   main   ref   ==   proxy   section   ice   ctime   write   

[TOC]

进程池

import requests, re, time
from multiprocessing.dummy import Pool
import random

IpPool = [{‘http‘: ‘183.147.230.104: 8118‘}, {‘http‘: ‘60.217.64.237: 31923‘},
          {‘http‘: ‘221.193.50.166: 8118‘}]


url = ‘https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=9&start=0‘

start = time.time()

video_list=[]

ret = requests.get(url)
reg = ‘<a href="(.*?)" class="vervideo-lilink actplay">‘
video_urls = re.findall(reg, ret.text)
print(video_urls)
for url in video_urls:
    proxy = random.choice(IpPool)
    ret_detail = requests.get(‘https://www.pearvideo.com/‘ + url, proxies=proxy)
    print(proxy)
    reg = ‘srcUrl="(.*?)",vdoUrl=srcUrl‘
    mp4_url = re.findall(reg, ret_detail.text)[0]  # type:str

    video_name = mp4_url.rsplit(‘/‘, 1)[-1]

    dic = {
        ‘v_name‘: video_name,
        ‘v_url‘: mp4_url
    }
    video_list.append(dic)

print(video_list)
def get_video(dic):
    url = dic[‘v_url‘]
    name = dic[‘v_name‘]
    print(f‘开始下载{name}‘)
    video_data = requests.get(url=url)
    print(url)
    with open(name, ‘wb‘) as f:
        for line in video_data.iter_content():
            f.write(line)
        print(f‘{name}下载完成‘)

    end = time.time()
    ctime = end - start
    print(ctime)

pools = Pool(12)
pools.map(get_video, video_list)
pools.close()
pools.join()

线程池

import requests
import re
import random
from concurrent.futures import ThreadPoolExecutor

import time
start = time.time()

pool = ThreadPoolExecutor(12)

IpPool = [{‘http‘: ‘183.147.230.104: 8118‘}, {‘http‘: ‘60.217.64.237: 31923‘},
          {‘http‘: ‘221.193.50.166: 8118‘}]

url = ‘https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=9&start=0‘


video_list=[]

ret = requests.get(url)
reg = ‘<a href="(.*?)" class="vervideo-lilink actplay">‘
video_urls = re.findall(reg, ret.text)
print(video_urls)
for url in video_urls:
    proxy = random.choice(IpPool)
    ret_detail = requests.get(‘https://www.pearvideo.com/‘ + url, proxies=proxy)
    print(proxy)
    reg = ‘srcUrl="(.*?)",vdoUrl=srcUrl‘
    mp4_url = re.findall(reg, ret_detail.text)[0]  # type:str

    video_name = mp4_url.rsplit(‘/‘, 1)[-1]

    dic = {
        ‘v_name‘: video_name,
        ‘v_url‘: mp4_url
    }
    video_list.append(dic)

def get_video(dic):
    url = dic[‘v_url‘]
    name = dic[‘v_name‘]
    print(f‘开始下载{name}‘)
    video_data = requests.get(url=url)
    print(url)
    with open(name, ‘wb‘) as f:
        for line in video_data.iter_content():
            f.write(line)
        print(f‘{name}下载完成‘)
        end = time.time()
        ctime = end - start
        print(ctime)

print(video_list)
def main():
    for url in video_list:
        done = pool.submit(get_video, url)


if __name__ == ‘__main__‘:
    main()
    pool.shutdown(wait=True)

梨视频,进程池、线程池爬取

标签:load   main   ref   ==   proxy   section   ice   ctime   write   

原文地址:https://www.cnblogs.com/kai-/p/12658461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!