import datetime
import json
import os
import random
import requests
import concurrent.futures
def extract_links_from_har_file(har_file_path, url_prefix):
with open(har_file_path, 'r', encoding='utf-8') as har_file:
har_data = json.load(har_file)
entries = har_data['log']['entries']
video_links = []
for entry in entries:
request_url = entry['request']['url']
if request_url.startswith(url_prefix):
video_links.append(request_url)
return video_links
def download_video(video_url, output_folder):
try:
response = requests.get(video_url, stream=True)
if response.status_code == 200:
video_filename = os.path.join(output_folder, generate_unique_filename())
with open(video_filename, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"Downloaded: {video_url}")
else:
print(f"Failed to download: {video_url}")
except Exception as e:
print(f"Error downloading {video_url}: {str(e)}")
def generate_unique_filename():
random_num = random.randint(1000, 9999)
current_date = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
return f"video_{current_date}_{random_num}.mp4"
def download_videos(video_links, output_folder, num_threads=35):
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(download_video, video_url, output_folder) for video_url in video_links]
for future in concurrent.futures.as_completed(futures):
try:
future.result()
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
har_file_path = r"你的har文件位置"
url_prefix = "你的请求的域名"
video_links = extract_links_from_har_file(har_file_path, url_prefix)
print(video_links)
output_folder = r"你的下载视频保存的位置"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
download_videos(video_links, output_folder)
确保已经安装了 requests
库。如果你没有安装它,你可以使用以下命令来安装:
pip install requests
这是我下载的截图