爱奇艺vip视频爬虫下载

 # -*- coding: UTF-8 -*-

import configparser
import time
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
import requests


class abuyun():
    config = configparser.ConfigParser()
    config.read("./config.ini")
    conf_abuyun = config["abuyun"]
    proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
        "host": conf_abuyun["proxyHost"],
        "port": conf_abuyun["proxyPort"],
        "user": conf_abuyun["proxyUser"],
        "pass": conf_abuyun["proxyPass"],
    }

    proxies = {
        "http": proxyMeta,
        "https": proxyMeta,
    }


def run(id):
    header = {
        "Origin": "https://jx.km58.top",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
        "Referer": "https://jx.km58.top/58/?url=https://www.iqiyi.com/v_19rra0h3wg.html",
    }

    id = str(id)
    if len(id) == 1:
        id = "00" + id
    elif len(id) == 2:
        id = "0" + id
    url = "https://cdn.ziyuan605.com/20190101/gapN0uwa/800kb/hls/SjDqfU3641{}.ts".format(id)
    response = requests.get(url, headers=header, verify=False)
    print(response.status_code)
    save_path = "./tss1/" + id + ".ts"  # 当前工作目录下的文件路径
    with open(save_path, 'wb') as code:
        code.write(response.content)
    print(id + ".ts [Download successful!]")


def main(thread_num=5):
    # 统计该爬虫的消耗时间
    print('*' * 50)
    start = time.time()

    # 利用并发下载
    executor = ThreadPoolExecutor(max_workers=thread_num)  # 可以自己调整max_workers,即线程的个数
    # submit()的参数: 第一个为函数, 之后为该函数的传入参数,允许有多个
    # ids = ["e27a4e2d-f7dc-442a-937e-6b10691275e5", "520ae730-20d2-11e6-bcc9-000c29ffef9b",
    #        "c01daf43-3c4d-11e7-866e-000c29ffef9b", "1faa27d9-f6f7-11e8-a44b-000c29ffef9b"]

    future_tasks = [executor.submit(run, id) for id in range(2135)]

    # 等待所有的线程完成,才进入后续的执行
    wait(future_tasks, return_when=ALL_COMPLETED)

    end = time.time()
    print("[All works are done.]")
    print('使用多线程,总共耗时:%s' % (end - start))
    print('*' * 50)


if __name__ == '__main__':
    main()

你可能感兴趣的:(爱奇艺vip视频爬虫下载)