python批量爬取视频并自动命名

    背景:视频只能通过扫码单次观看, 网站无法开启F12直接查看源代码:

调用download_file函数下载网页源代码

根据txt行数提取出视频文件url链接和文件title。批量自动更名下载:

from contextlib import closing

import linecache
import requests
import os

def getvideo_url(path):
    line = linecache.getline(path, 150)
    print(line)
    with open('D:/title.txt', "a") as f:
        f.write(line + '\n')

def download_file(url, path):
    with closing(requests.get(url, stream=True)) as r:
        chunk_size = 1024
        # content_size = int(r.headers['content-length'])
        print ('下载开始')
        with open(path, "wb") as f:
            n = 1
            for chunk in r.iter_content(chunk_size=chunk_size):
                f.write(chunk)
                n += 1

def download_mp4():
    url_file = 'D:/url.txt'
    title_file = 'D:/title.txt'
    fileopen = open(url_file, 'r')    # 视频url链接
    titleopen = open(title_file, 'r')    # 视频文件名

    for line in fileopen:
        line = line.strip('\n')
        for title in titleopen:
            titleline = title.strip('\n')
            video_path = 'D:\\video1111\\' + titleline + '.mp4'
            request = requests.get(line)
            with open(video_path, 'wb') as f:
                f.write(request.content)
            break
        print(video_path)
    fileopen.close()


if __name__ == '__main__':
    path = 'D:/video/'
    '''
    for i in range (3, 99):
        if i < 10:
            url = "http://www.proedu.com.cn/web/shareVideo/index.action?id=101340" + str(i)
        else:
            url = "http://www.proedu.com.cn/web/shareVideo/index.action?id=10134" + str(i)
        download_file(url, path + str(i))
    
    for i in range(3, 99):
        getvideo_url('D:/video/'+ str(i))
    '''
    download_mp4()
    print("success")

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值