背景:视频只能通过扫码单次观看, 网站无法开启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")